2011-08-08 38 views
20

时间服务器是否仅在服务器上以默认语言工作?下面的代码应该解析为2005年8月11日,但它使用法语“aout”而不是英文“aug”。strtotime使用不同的语言?

任何想法如何处理?

<?php 
    $date = strtotime('11 aout 05'); 
    echo date('d M Y',$date); 
?> 

回答

7

docs

解析任何英文文本的日期时间描述为Unix 时间戳

+1

这应该是公认的答案。 – Spir

+21

它不回答“任何想法如何处理这个问题?” –

+5

不解决问题 – onassar

-4

尝试转换之前设置的语言环境:

setlocale(LC_TIME, "fr_FR"); 
-3

这是区域依赖。如果它必须检查每个语言的每一个解析,那么它就要花费很长时间才能解析即使是最简单的日期字符串。

如果你有与已知格式的字符串,可以考虑使用date_create_from_format(),which'll可以更加高效和更少的错误打印

+0

这是错误的。 DateTime不会解析非英文数据。即这将失败: setlocale(LC_TIME,“fr_FR”); $ dt = DateTime :: createFromFormat('d F Y',“12Août2013”​​); – Spir

9

如前所述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).

请注意,根据您的系统,语言环境和编码,你将不得不考虑重音字符。

+0

从文档中还要注意:_这个函数没有在Windows平台上实现,这个函数可以在不同的操作系统中有不同的行为,并且你至少需要php 5.1。 – PhoneixS

9

法国月份日期为:

维耶FEVRIER火星艾薇麦朱安货币类型AOUT septembre OCTOBRE NOVEMBREdé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'))); } 

该函数无论如何不会中断,如果你通过$ da te_string英文,因为它不会做任何替换。

7

这个方法,你应该使用strftime的工作:

setlocale (LC_TIME, "fr_FR.utf8"); //Setting the locale to French with UTF-8 

echo strftime(" %d %h %Y",strtotime($date)); 

strftime

4

我写了一个简单的功能,部分解决了这个问题。 它不能用作完整的strtotme(),但它确定日期中的月份名称数。

<?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) 

同样,我们可以继续确定一周中的数字和天数等。

功能:

<?php 

function month_to_number($month, $locale_set = 'ru_RU') 
{ 
    $month = mb_convert_case($month, MB_CASE_LOWER, 'UTF-8'); 
    $month = preg_replace('/я$/', 'й', $month); // fix for 'ru_RU' 
    $locale = 
     setlocale(LC_TIME, '0'); 
     setlocale(LC_TIME, $locale_set.'.UTF-8'); 

    $month_number = FALSE; 

    for ($i = 1; $i <= 12; $i++) 
    { 
     $time_month  = mktime(0, 0, 0, $i, 1, 1970); 
     $short_month = date('M', $time_month); 
     $short_month_lc = strftime('%b', $time_month); 

     if (stripos($month, $short_month) === 0 OR 
      stripos($month, $short_month_lc) === 0) 
     { 
      $month_number = sprintf("%02d", $i); 

      break; 
     } 
    } 

    setlocale(LC_TIME, $locale); // return locale back 

    return $month_number; 
} 
2

解决这个问题的关键是外国文字的格式转换成他们的英国同行。我也需要这个,因为已经给出了答案,我写了一个很好的,干净的函数,它可以用来检索英文月份名称。

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 

您可以调整这一天的星期几。

+0

将完整的日期字符串传递给它后,我发现你可以在同一个函数中进行月份名称替换和日期名称替换。 –

0

添加此作为Marco Demaio answer的扩展版本。每周添加法语天数和月份缩写:

<?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', 
     ] 
    ) 
); 
}