Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中解析本地化的日期字符串_Php_Localization - Fatal编程技术网

在PHP中解析本地化的日期字符串

在PHP中解析本地化的日期字符串,php,localization,Php,Localization,我有一些代码(它是wordpress插件的一部分),它接受一个文本字符串和date()的格式说明符,并尝试将其解析为一个包含小时、分钟、秒、天、月、年的数组 目前,我使用以下代码(注意strotime在01/02/03之类的情况下非常不可靠) 这实际上效果相当好,似乎可以处理我抛出的每一个组合 然而,最近有人给了我2010年11月24日。这是2010年11月24日的俄文[日期格式为j F,Y],解析为年=2010,月=null,日=24 我是否可以使用任何函数,知道如何将11月和11月转换为11

我有一些代码(它是wordpress插件的一部分),它接受一个文本字符串和date()的格式说明符,并尝试将其解析为一个包含小时、分钟、秒、天、月、年的数组

目前,我使用以下代码(注意strotime在01/02/03之类的情况下非常不可靠)

这实际上效果相当好,似乎可以处理我抛出的每一个组合

然而,最近有人给了我
2010年11月24日
。这是2010年11月24日的俄文[日期格式为
j F,Y
],解析为年=2010,月=null,日=24

我是否可以使用任何函数,知道如何将11月和11月转换为11月

编辑:

运行
print_r(setlocale(LC_ALL,0))返回
C
。切换回
strtime()
似乎可以解决问题,但文档警告:

在内部,此函数调用系统的C库提供的strtime()函数。此函数在不同的操作系统中表现出明显不同的行为。建议在PHP5.3.0及更高版本上使用date_parse_from_format(),它不会遇到这些问题


date\u parse\u from\u format()

尝试将区域设置设置为俄语:

月份和工作日名称以及其他依赖于语言的字符串与使用
(LC_TIME
)设置的当前语言环境有关


在进行日期解析之前,您可以尝试使用locale参数并调用locale\u set\u default($locale)

$originalLocale = locale_get_default();
$locale ? $locale : $originalLocale;
locale_set_default(locale);

// date parsing code

locale_set_default($originalLocale);
我还没有测试过这个,但这是一个尝试。
仅供参考,我相信俄语的区域设置字符串是“ru Latn”

我看到这个问题已经得到了回答,但没有一个解决方案对我有效

这是我的解决方案:

if(!preg_match('/^en_US/', $locale)){

    $months_short = array('jan' => t('jan'), 'feb' => t('feb'), 'mar' => t('mar'), 'apr' => t('apr'),
            'may' => t('may'), 'jun' => t('giu'), 'jul' => t('lug'), 'aug' => t('ago'),
            'sep' => t('set'), 'oct' => t('ott'), 'nov' => t('nov'), 'dec' => t('dec'));

    foreach ($months_short as $month_short => $month_short_translated) {
        $date = preg_replace('/'.$month_short_translated.'/', $month_short, strtolower($date));
    }

}

$pieces = date_parse_from_format($format,$date);

if($pieces && $pieces['error_count'] == 0 && checkdate($pieces['month'], $pieces['day'], $pieces['year'])){

    return date('Y-m-d', mktime(0,0,0,$pieces['month'],$pieces['day'],$pieces['year'])); 

}
其中t()返回月份的翻译缩写


可能不是最好的解决方案(因为如果没有有效的翻译,它就会失败),但它对我的情况有效。

我的问题是我不知道语言环境应该是什么,我假设它已经设置好了。问题是它是一个其他人可能会使用的插件。。。找到一个“合适的”解决方案会很好。但是我想我还是坚持使用
strtime()
,直到我找到一个不起作用的例子…@Mikeage一个UnitTest是合适的:)如果
date\u create\u from\u format
可以解析本地化字符串,你可以试试。我不认为是这样,但还是尝试一下。我尝试了strtime()和date_create_from_格式解析意大利日期,如2013年6月18日(=2013-05-18),但没有成功。@dam尝试这里提供的解决方案:@Gordon我有机会尝试,但没有,我找不到任何方法使用IntlDateFormatter从意大利语转换为英语。不过,从英语到意大利语是可能的。基本上使用IntlDateFormatter:-从“2014-09-12”可以有“2014年12集”或“2014年12集”(这些是意大利语)-从“2014年12集”或“2014年12集”不能有“2014-09-12”。你需要先把“Set”翻译成“Sep”
if(!preg_match('/^en_US/', $locale)){

    $months_short = array('jan' => t('jan'), 'feb' => t('feb'), 'mar' => t('mar'), 'apr' => t('apr'),
            'may' => t('may'), 'jun' => t('giu'), 'jul' => t('lug'), 'aug' => t('ago'),
            'sep' => t('set'), 'oct' => t('ott'), 'nov' => t('nov'), 'dec' => t('dec'));

    foreach ($months_short as $month_short => $month_short_translated) {
        $date = preg_replace('/'.$month_short_translated.'/', $month_short, strtolower($date));
    }

}

$pieces = date_parse_from_format($format,$date);

if($pieces && $pieces['error_count'] == 0 && checkdate($pieces['month'], $pieces['day'], $pieces['year'])){

    return date('Y-m-d', mktime(0,0,0,$pieces['month'],$pieces['day'],$pieces['year'])); 

}