I need to parse a date string in a multi-language application. Every user has their locale and then their date format.
How to use:
new DateTime($datestr);
or
date_parse($datestr);
with the localized date format?
Assuming mm/dd/yyyy as EN date format and dd/mm/yyyy as IT date format I did this test:
<?php
echo "EN locale<br>\r\n";
setlocale(LC_ALL, 'en_US');
$date="01/02/2015"; //2th Jan
$date_array=date_parse($date);
$mktime=mktime($date_array['hour'], $date_array['minute'], $date_array['second'], $date_array['month'], $date_array['day'], $date_array['year']);
$datetime=new DateTime($date);
echo date("Y-m-d",$mktime);
echo "<br>\r\n";
echo $datetime->format('Y-m-d');
echo "<br>\r\n";
echo "IT locale<br>\r\n";
setlocale(LC_ALL, 'it_IT');
$date="01/02/2015"; //1th Feb
$date_array=date_parse($date);
$mktime=mktime($date_array['hour'], $date_array['minute'], $date_array['second'], $date_array['month'], $date_array['day'], $date_array['year']);
$datetime=new DateTime($date);
echo date("Y-m-d",$mktime);
echo "<br>\r\n";
echo $datetime->format('Y-m-d');
The result is the SAME output, with both locale settings the parse is provided with the mm/dd/yyyy format. The output was always 2015-01-02 (2nd Feb)
This is the answer:
And this is the previous test working with my answer.
Unfortunately I cannot earn my bounty... :-)
These are the changes but you should be able to copy and paste the code above and it will work the way you want.
referance http://php.net/manual/en/function.date.php