Cor*_*Dee 24 php localization strtotime
strtotime只能在服务器上使用默认语言吗?下面的代码应该解决到2005年8月11日,但它使用法语"aout"而不是英语"aug".
任何想法如何处理这个?
<?php
$date = strtotime('11 aout 05');
echo date('d M Y',$date);
?>
Run Code Online (Sandbox Code Playgroud)
Tec*_*noh 10
如上所述strtotime
,不考虑区域设置.但是你可以使用strptime
(参见http://ca1.php.net/manual/en/function.strptime.php),因为根据文档:
Month and weekday names and other language dependent strings respect the current locale set with setlocale() (LC_TIME).
请注意,根据您的系统,区域设置和编码,您必须考虑重音字符.
Mar*_*aio 10
法国月份日期是:
janvierfévriermarsavril mai juin juilletaoûtseptembreoctobrenovembredécembre
因此,对于非常具体的情况,月份是法语,你可以使用
function myStrtotime($date_string) { return strtotime(strtr(strtolower($date_string), array('janvier'=>'jan','février'=>'feb','mars'=>'march','avril'=>'apr','mai'=>'may','juin'=>'jun','juillet'=>'jul','août'=>'aug','septembre'=>'sep','octobre'=>'oct','novembre'=>'nov','décembre'=>'dec'))); }
Run Code Online (Sandbox Code Playgroud)
如果你用英语传递$ date_string,那么函数无论如何都不会中断,因为它不会做任何替换.
来自文档
将任何英文文本日期时间描述解析为Unix时间戳
编辑:现在已经有六年了,而且为什么strtotime()对于手头的问题是不恰当的解决方案的原因是什么意思成为接受的答案
为了更好地回答实际问题,我想回应Marc B的回答:尽管有downvotes,date_create_from_format,与自定义Month解释器配对将提供最可靠的解决方案
然而,似乎目前还没有内置于PHP的国际日期解析的子弹.
此方法应该适用于您strftime
:
setlocale (LC_TIME, "fr_FR.utf8"); //Setting the locale to French with UTF-8
echo strftime(" %d %h %Y",strtotime($date));
Run Code Online (Sandbox Code Playgroud)
解决这个问题的关键是将外国文本表示转换为英文对应内容。我也需要这个,所以受到已经给出的答案的启发,我编写了一个漂亮而干净的函数,可以用于检索英文月份名称。
function getEnglishMonthName($foreignMonthName, $setLocale='nl_NL'){
$originalLocale = Locale::getDefault();
setlocale(LC_ALL, 'en_US');
$monthNumbers = range(1,12);
foreach($monthNumbers as $month)
$englishMonths[] = strftime('%B',mktime(0,0,0,$month,1,2011));
setlocale(LC_ALL, $setLocale);
foreach($monthNumbers as $month)
$foreignMonths[] = strftime('%B',mktime(0,0,0,$month,1,2011));
return str_replace($foreignMonths, $englishMonths, $foreignMonthName);
setlocale(LC_ALL, $originalLocale)
}
echo getEnglishMonthName('juli');
// Outputs July
Run Code Online (Sandbox Code Playgroud)
您也可以针对一周中的几天以及任何其他区域设置进行调整。