strtotime与不同的语言?

时间:2011-08-08 21:01:38

标签: php localization strtotime

strtotime只能在服务器上使用默认语言吗?下面的代码应该解决到2005年8月11日,但它使用法语“aout”而不是英语“aug”。

任何想法如何处理?

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

8 个答案:

答案 0 :(得分: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).

请注意,根据您的系统,区域设置和编码,您必须考虑重音字符。

答案 1 :(得分:10)

法国月份日期是:

  

janvierfévriermarsavril mai juinjuilletaoûtseptembreoctobre   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'))); }

如果你用英语传递$ date_string ,这个函数不会中断,因为它不会做任何替换。

答案 2 :(得分:8)

来自docs

  

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

编辑:现在已经过了六年了,为什么strtotime()对于手头的问题不合适的解决方案成为接受的答案是什么意思?

为了更好地回答实际问题,我想回应Marc B的回答:尽管存在downvotes,date_create_from_format,与自定义Month解释器配对将提供最可靠的解决方案

然而,似乎暂时还没有内置于PHP的国际日期解析的灵丹妙药。

答案 3 :(得分:8)

此方法适用于strftime

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

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

strftime

答案 4 :(得分: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;
}

答案 5 :(得分: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

您可以针对一周中的几天以及任何其他语言环境调整此值。

答案 6 :(得分:-3)

它依赖于语言环境。如果它必须为每个解析检查每种语言,那么即使是最简单的日期字符串,也需要用FOREVER解析。

如果您有一个已知格式的字符串,请考虑使用date_create_from_format(),这样效率会更高,错误打印更少

答案 7 :(得分:-5)

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

setlocale(LC_TIME, "fr_FR");