我有一些代码(它是wordpress插件的一部分),它接受一个文本字符串,并给出date()的格式说明符,并尝试将其解析为包含小时,分钟,秒,日,月,年的数组.
目前,我使用以下代码(请注意strtotime与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;
}
Run Code Online (Sandbox Code Playgroud)
这实际上运作得相当好,似乎处理了我抛出的每一个组合.
然而,最近有人给了我24 ??????, 2010.这是2010年11月24日的俄语[日期格式为j F, Y],它被解析为年份= 2010年,月份= null,日期= 24.
是否有任何我可以使用的功能知道如何将11月和Ноябрь翻译成11?
编辑:
运行print_r(setlocale(LC_ALL, 0));回报C.切换回strptime()似乎可以解决问题,但文档警告:
在内部,此函数调用系统的C库提供的strptime()函数.此功能可以在不同的操作系统上表现出明显不同的行为.在PHP 5.3.0及更高版本中,建议使用不受这些问题影响的date_parse_from_format().
是date_parse_from_format()正确的API,如果是这样,我如何让它识别语言?