i have array where one of the key is datetime , and its value format looks like this :
dec/10/2017 01:50:33
and my problem is , when i insert to database , the value changes become like :
0000-00-00 00:00:00
Could you help me to fix that ?? how to fix that in php, Thanks
Use str_replace
and date
and strtotime
method
Easy and Simple like this:
$date = 'dec/10/2017 01:50:33';
$formatedDate = str_replace('/', '-', $date);
echo date('Y-m-d H:i:s',strtotime($formatedDate));
Output:
2017-12-10 01:50:33
This is pretty easy to do if you just reformat the string so that PHP's strtotime function knows what to do with it:
<?php
// Original string
$str = "dec/10/2017 01:50:33";
// Split the date from the time
$parts = explode(' ', $str);
// Split up the date
$date_parts = explode( '/', $parts[0] );
// Reassemble so strtotime works with a recognized format
$time = strtotime( $date_parts[0] . ' ' . $date_parts[1] . ', ' . $date_parts[2] . ' ' . $parts[1] );
// Format date for MySQL
echo date('Y-m-d H:i:s', $time);
// 2017-12-10 01:50:33