Parsing localized date strings in PHP

Posted by Mikeage on Stack Overflow See other posts from Stack Overflow or by Mikeage
Published on 2011-01-11T07:46:12Z Indexed on 2011/01/12 5:53 UTC
Read the original article Hit count: 176

Filed under:
|
|

Hi,

I have some code (it's part of a wordpress plugin) which takes a text string, and the format specifier given to date(), and attempts to parse it into an array containing hour, minute, second, day, month, year.

Currently, I use the following code (note that strtotime is horribly unreliable with things like 01/02/03)

// $format contains the string originally given to date(), and $content is the rendered string
if (function_exists('date_parse_from_format')) {
    $content_parsed = date_parse_from_format($format, $content);
} else {
    $content = preg_replace("([0-9]st|nd|rd|th)","\\1",$content);
    $content_parsed = strptime($content, dateFormatToStrftime($format));
    $content_parsed['hour']=$content_parsed['tm_hour'];
    $content_parsed['minute']=$content_parsed['tm_min'];
    $content_parsed['day']=$content_parsed['tm_mday'];
    $content_parsed['month']=$content_parsed['tm_mon'] + 1;
    $content_parsed['year']=$content_parsed['tm_year'] + 1900;
}

This actually works fairly well, and seems to handle every combination I've thrown at it.

However, recently someone gave me 24 ??????, 2010. This is Russian for November 24, 2010 [the date format was j F, Y], and it is parsed as year = 2010, month = null, day = 24.

Are there any functions that I can use that know how to translate both November and ?????? into 11?

EDIT:

Running print_r(setlocale(LC_ALL, 0)); returns C. Switching back to strptime() seems to fix the problem, but the docs warn:

Internally, this function calls the strptime() function provided by the system's C library. This function can exhibit noticeably different behaviour across different operating systems. The use of date_parse_from_format(), which does not suffer from these issues, is recommended on PHP 5.3.0 and later.

Is date_parse_from_format() the correct API, and if so, how do I get it to recognize the language?

© Stack Overflow or respective owner

Related posts about php

Related posts about php5