I want to convert in php a datetime fr ( 30/11/2016 12:30 ) to a datetime US (2016-11-30 12:30)
Should I use explode then implode or is there a better solution to convert it ?
I want to convert in php a datetime fr ( 30/11/2016 12:30 ) to a datetime US (2016-11-30 12:30)
Should I use explode then implode or is there a better solution to convert it ?
I highly discourage use of "String Functions(str_replace
etc.)" to modify datetime strings. Even procedural way should be avoided.
Correct way is to use DateTime class and ofcourse OOP way:
$time = '30/11/2016 12:30';
$date = DateTime::createFromFormat('d/m/Y H:i', $time);//assuming you are using 24 hour format for time
$time = $date->format('Y-m-d H:i');
echo $time;//2016-11-30 12:30
For more insight, please check: http://php.net/manual/en/datetime.createfromformat.php
I hope it helps
$french_date_string = '30/11/2016 12:30';
$french_date_string = str_replace('/', '-', $french_date_string);
$date = new DateTime($french_date_string);
echo $date->format('Y-m-d H:i');
This code also works. But as far as I'm concerned, Abhay Maurya's answer seems to be more reliable and perfect way of working with datetime with the dedicated datetime classes and functions.