18

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)

Tobia
  • 9,165
  • 28
  • 114
  • 219
  • see this similar question about what you need http://stackoverflow.com/questions/8827514/get-date-format-according-to-the-locale-in-php. you will need to use `set_locale` along with `getDateFormat` – Alex Andrei Aug 28 '15 at 05:22
  • Why I cannot find any docs about getDateFormat()? – Tobia Aug 28 '15 at 06:19
  • apologies @Tobia, I was in a rush writing my comment, getDateFormat, is a custom function that will return the date format depending on the locale – Alex Andrei Aug 28 '15 at 06:23

2 Answers2

9

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.

<?php
echo "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... :-)

Tobia
  • 9,165
  • 28
  • 114
  • 219
-2
<?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("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');

referance http://php.net/manual/en/function.date.php

kayleighsdaddy
  • 670
  • 5
  • 15
  • 1
    Are you kidding me? Your solution is to change manually the date format... I want to parse the date according to the user locale that can be anyone. In my question I wrote this code `echo date("Y-m-d",$mktime);` to show that with differente locale the parsed date was the same. – Tobia Aug 28 '15 at 06:16
  • I do not think there is a way in php to automate what you want automated. I think it has to be done manually. – kayleighsdaddy Aug 28 '15 at 06:31
  • It would be a shame... because the opposite process (format a date according to user's locale) exists, so the locale date format information exists somewhere in php libraries. – Tobia Aug 28 '15 at 07:10
  • Javascript has it, so there really wouldn't be a need. toLocaleString() in javascript should give the output you are looking for. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString – kayleighsdaddy Aug 28 '15 at 19:29