PHP: Parse date from localized format
This is the answer:
$formatter = new IntlDateFormatter("en_US", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);$unixtime=$formatter->parse($date);
And this is the previous test working with my answer.
<?phpecho "EN locale<br>\r\n";$date="01/02/2015"; //2th Jan$formatter = new IntlDateFormatter("en_US", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);$unixtime=$formatter->parse($date);$datetime=new DateTime();$datetime->setTimestamp($unixtime);echo $datetime->format('Y-m-d');echo "<br>\r\n";echo "IT locale<br>\r\n";$date="01/02/2015"; //1th Feb$formatter = new IntlDateFormatter("it_IT", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);$unixtime=$formatter->parse($date);$datetime=new DateTime();$datetime->setTimestamp($unixtime);echo $datetime->format('Y-m-d');echo "<br>\r\n";
Unfortunately I cannot earn my bounty... :-)
<?phpecho "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("m/d/Y",$mktime);echo "<br>\r\n";echo $datetime->format('m/d/Y');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("d/m/Y",$mktime);echo "<br>\r\n";echo $datetime->format('d/m/Y');
These are the changes but you should be able to copy and paste the code above and it will work the way you want.
echo date("d/m/Y",$mktime);echo date("m/d/Y",$mktime);echo $datetime->format('m/d/Y');echo $datetime->format('d/m/Y');