4

I'm trying to make something clever in order to parse date in any international format.

On my frontend I use jquery ui as a date picker and each languages has its specific date format.

Fr: d/m/Y En: m/d/Y ...

OK but now on the php part I have to store those date using the same format (Mysql Y-m-d). I would like to avoid using switch case and stuff like that so I started to look for another alternative.

The best thing I've found is

http://www.php.net/manual/en/datetime.createfromformat.php That function will enable me to parse the dates if I know the format.

For example

$format = 'Y-m-d';
$date = DateTime::createFromFormat($format, '2009-02-15');

Ok so that's a good start but now I'd like to find the standard date format for the current locale.

So I found this :

http://www.php.net/manual/en/intldateformatter.getpattern.php

But I dont really know how to get it to work because I get inconstitent results:

php > $i = new IntlDateFormatter("fr_FR.utf8",IntlDateFormatter::SHORT,IntlDateFormatter::NONE);
php > echo $i->getPattern();
dd/MM/yy
/* correct ! */

but

php > $i = new IntlDateFormatter("en_US.utf8",IntlDateFormatter::SHORT,IntlDateFormatter::NONE);
php > echo $i->getPattern();                                                    
M/d/yy /* wtf */

Am I missing something ?

Thanks

Paté
  • 1,914
  • 2
  • 22
  • 33
  • 8
    At some point, you have to put down your foot and settle on a format yourself. Or you'll end up trying to figure out what 1/2/3 is. Feb 1, '03? 3rd Feb, '01? Mar 1, '02? Blah blah blah. It's nice to try and be accomodating, but sometimes you have to insist that things be in a certain format. – Marc B Sep 21 '11 at 19:31
  • The format that will be stored will be in a specific format but I need to parse the date passed by the user. – Paté Sep 21 '11 at 19:33
  • 2
    Yes, but again... if you allow things like 1/2/3 from the user, you can't reliably parse that. Like I said, being flexible on inputs is all fine and dandy, but at some point it's easier to insist on a fixed format instead of trying to bend yourself into a knot. – Marc B Sep 21 '11 at 19:35
  • The thing is that since I use a datepicker, format is actually forced in the right locale format so I tought that would be good enough to trust – Paté Sep 21 '11 at 19:39
  • If you can figure out how to force the datepicker to submit in one single format, your life would be a lot easier. – Marc B Sep 21 '11 at 19:41
  • 1
    Ok you saved my day http://jqueryui.com/demos/datepicker/#option-altFormat – Paté Sep 21 '11 at 19:45
  • http://php.net/manual/en/function.strtotime.php – dqhendricks Sep 21 '11 at 21:19
  • @Paté: Yup, use a date picker. If Excel can't do it, you better take that for granted. – Alix Axel Sep 21 '11 at 22:32
  • You can accept 1/2/3 if you select a locale date format. For example, if locale is it_IT, 1/2/3 should be parsed as dd/mm/yy that is, 1° febbraio 1903. – Dario de Judicibus Jun 19 '22 at 17:58

1 Answers1

1

There is no "universal date format symbol standard". The symbol "y" could be a 2-digit year, a 4-digit year, or even a 3-letter abbreviated month (highly unlikely), depending on the source.

The formatting symbols for IntlDateFormatter are documented here. As you can see from the documentation, for this implementation, "d" is the date without leading zeros, while "dd" contains the leading zeros. Both "M" and "MM" represent the month with leading zeros. The "yy" is the 2-digit year.

There is enough difference between this and jQuery UI's date format symbols that you'll need a data map to map the IntlDateFormatter format symbols to jQuery UI datepicker format symbols, and vice versa.

Something like this should be sufficient (untested):

// PHP => jQuery
$formatMap = array(
    'dd' => 'dd',
    'd'  => 'd',
    'MM' => 'mm',
    'M'  => 'mm',
    'yy' => 'y'
    // and so on...
);

Once you have your map set up, you can create your IntlDateFormatter based on locale, convert that format's symbols into jQuery UI date format symbols, and then send the format to the front-end.

When the user posts back the chosen date, you can do the same in reverse to get back to IntlDateFormatter symbols. At that point, you can use a combination of IntlDateFormatter::setPattern() to set the format to MySQL-style, and datefmt_format() (or equivalent) to actually format the posted date.

Hope this helps.

FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107