Php 不同语言的时间?
strotime是否仅在服务器上以默认语言工作?以下代码应解析为2005年8月11日,但它使用法语“aout”而不是英语“aug” 你知道怎么处理吗Php 不同语言的时间?,php,localization,strtotime,Php,Localization,Strtotime,strotime是否仅在服务器上以默认语言工作?以下代码应解析为2005年8月11日,但它使用法语“aout”而不是英语“aug” 你知道怎么处理吗 <?php $date = strtotime('11 aout 05'); echo date('d M Y',$date); ?> 来自 将任何英文文本datetime描述解析为Unix 时间戳 编辑:六年过去了,关于为什么strotime()对于当前问题是不合适的解决方案的旁注变成了公认的答案在转换之前尝试设置
<?php
$date = strtotime('11 aout 05');
echo date('d M Y',$date);
?>
来自
将任何英文文本datetime描述解析为Unix
时间戳
编辑:六年过去了,关于为什么strotime()对于当前问题是不合适的解决方案的旁注变成了公认的答案在转换之前尝试设置区域设置:
setlocale(LC_TIME, "fr_FR");
它取决于语言环境。如果每次解析都要检查每种语言,那么解析哪怕是最简单的日期字符串也要花很长时间
如果你有一个已知格式的字符串,考虑使用,这将是更有效和更少的错误打印< < /p> 如<代码> > StrtoTime/COD>不考虑区域设置。但是,您可以使用strtime(请参阅),因为根据文档:
Month和weekday名称以及其他依赖于语言的字符串遵守使用setlocale()设置的当前区域设置(LC_TIME)。
请注意,根据您的系统、区域设置和编码,您必须考虑重音字符。法语月份日期为: janvier février mars avril mai juin juillet九月份 诺文布雷酒店 因此,对于月份为法语的非常具体的情况,您可以使用
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'))); }
如果您用英语传递$date\u字符串,函数无论如何不会中断,因为它不会进行任何替换。此方法适用于您使用
strftime
:
setlocale (LC_TIME, "fr_FR.utf8"); //Setting the locale to French with UTF-8
echo strftime(" %d %h %Y",strtotime($date));
我编写了一个简单的函数,部分解决了这个问题。 它不能作为完整的strotme(),但它确定日期中的月数名称
<?php
// For example, I get the name of the month from a
// date "1 January 2015" and set him (with different languages):
echo month_to_number('January').PHP_EOL; // returns "01" (January)
echo month_to_number('Января', 'ru_RU').PHP_EOL; // returns "01" (January)
echo month_to_number('Мая', 'ru_RU').PHP_EOL; // returns "05" (May)
echo month_to_number('Gennaio', 'it_IT').PHP_EOL; // returns "01" (January)
echo month_to_number('janvier', 'fr_FR').PHP_EOL; // returns "01" (January)
echo month_to_number('Août', 'fr_FR').PHP_EOL; // returns "08" (August)
echo month_to_number('Décembre', 'fr_FR').PHP_EOL; // returns "12" (December)
解决这个问题的关键是将外国文本表示转换为英语对应形式。我也需要这个,所以受已经给出的答案的启发,我编写了一个漂亮而干净的函数,用于检索英文月份名称
function getEnglishMonthName($foreignMonthName,$setlocale='nl_NL'){
setlocale(LC_ALL, 'en_US');
$month_numbers = range(1,12);
foreach($month_numbers as $month)
$english_months[] = strftime('%B',mktime(0,0,0,$month,1,2011));
setlocale(LC_ALL, $setlocale);
foreach($month_numbers as $month)
$foreign_months[] = strftime('%B',mktime(0,0,0,$month,1,2011));
return str_replace($foreign_months, $english_months, $foreignMonthName);
}
echo getEnglishMonthName('juli');
// Outputs July
您还可以针对一周中的几天以及任何其他区域设置对此进行调整。将其添加为的扩展版本。增加了法语“一周中的几天”和“几个月”缩写:
<?php
public function frenchStrtotime($date_string) {
$date_string = str_replace('.', '', $date_string); // to remove dots in short names of months, such as in 'janv.', 'févr.', 'avr.', ...
return strtotime(
strtr(
strtolower($date_string), [
'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',
'janv'=>'jan',
'févr'=>'feb',
'avr'=>'apr',
'juil'=>'jul',
'sept'=>'sep',
'déc'=>'dec',
'lundi' => 'monday',
'mardi' => 'tuesday',
'mercredi' => 'wednesday',
'jeudi' => 'thursday',
'vendredi' => 'friday',
'samedi' => 'saturday',
'dimanche' => 'sunday',
]
)
);
}
这是错误的。DateTime不会解析非英语数据。ie这将失败:setlocale(LC_TIME,“fr_fr”)$dt=DateTime::createFromFormat('DFY',“2013年12月”);从文档中还可以注意到:此函数不是在Windows平台上实现的,此函数在不同的操作系统中可能有不同的行为,您至少需要php 5.1。由于strtime()
不接受区域设置日期格式作为参数,所以它有点无用,因此,您不能使用strtime()
以另一种格式对区域设置日期进行编码。你能做的是将通用格式编码到语言环境中(但不是反之亦然)。它不回答“有什么想法如何处理这个问题?”的问题,也不解决问题,也不提供解决方案。我总是听“英语”。。。。请存在更多的语言!有西班牙语/加泰罗尼亚语的解析吗?任何类似于strotime(“2016年3月16日”,“ca”)或类似的名称?您的答案中没有任何概念。在向其传递完整的日期字符串后,我发现您可以在同一函数中进行月名替换和日名替换。
<?php
public function frenchStrtotime($date_string) {
$date_string = str_replace('.', '', $date_string); // to remove dots in short names of months, such as in 'janv.', 'févr.', 'avr.', ...
return strtotime(
strtr(
strtolower($date_string), [
'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',
'janv'=>'jan',
'févr'=>'feb',
'avr'=>'apr',
'juil'=>'jul',
'sept'=>'sep',
'déc'=>'dec',
'lundi' => 'monday',
'mardi' => 'tuesday',
'mercredi' => 'wednesday',
'jeudi' => 'thursday',
'vendredi' => 'friday',
'samedi' => 'saturday',
'dimanche' => 'sunday',
]
)
);
}