php strtotime 使用不同的语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6988536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:45:27  来源:igfitidea点击:

strtotime With Different Languages?

phplocalizationstrtotime

提问by Cory Dee

Does strtotime only work in the default language on the server? The below code should resolve to august 11, 2005, however it uses the french "aout" instead of the english "aug".

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

Any ideas how to handle this?

任何想法如何处理这个?

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

采纳答案by Mr Griever

From the docs

文档

Parse about any English textual datetime description into a Unix timestamp

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

Edit: Six years down the road now, and what was meant to be a side-note about why strtotime() was an inappropriate solution for the issue at hand became the accepted answer

编辑:现在已经过去了六年,关于为什么 strtotime() 对手头的问题来说是一个不合适的解决方案的旁注成为了公认的答案

To better answer the actual question I want to echo Marc B's answer: despite the downvotes, date_create_from_format, paired with a custom Month interpreter will provide the most reliable solution

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

However it appears that there is still no silver-bullet for international date parsing built-in to PHP for the time being.

然而,目前似乎还没有内置到 PHP 中的国际日期解析的灵丹妙药。

回答by Marco Demaio

French month dates are:

法国月份的日期是:

janvier février mars avril mai juin juillet ao?t septembre octobre novembre décembre

janvier février mars avril mai juin juillet ao?t 九月十月十一月十二月

Hence, for the very specific case where months are in French you could use

因此,对于月份是法语的非常特殊的情况,您可以使用

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

The function anyway does not break if you pass $date_string in English, because it won't do any substitution.

如果您在 English 中传递 $date_string ,该函数无论如何都不会中断,因为它不会进行任何替换。

回答by Technoh

As mentioned strtotimedoes not take locale into account. However you could use strptime(see http://ca1.php.net/manual/en/function.strptime.php), since according to the docs:

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

Month and weekday names and other language dependent strings respect the current locale set with setlocale() (LC_TIME).

Note that depending on your system, locale and encoding you will have to account for accented characters.

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

回答by Rashad

This method should work for you using strftime:

此方法应该适用于您使用strftime

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

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

strftime

时间

回答by DmitryS

I wrote a simple function partially solves this problem. It does not work as a full strtotme(), but it determines the number of months names in the dates.

我写了一个简单的函数部分解决了这个问题。它不能作为完整的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)

Similarly, we can proceed to determine the numbers and days of the week, etc.

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

Function:

功能:

<?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;
}

回答by Ogier Schelvis

The key to solving this question is to convert foreign textual representations to their English counterparts. I also needed this, so inspired by the answers already given I wrote a nice and clean function which would work for retrieving the English month name.

解决这个问题的关键是将外国文本表示转换为英文文本。我也需要这个,所以受到已经给出的答案的启发,我写了一个漂亮而干净的函数,它可以用于检索英文月份名称。

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

You can adjust this for days of the week aswell and for any other locale.

您也可以针对一周中的几天和任何其他语言环境进行调整。

回答by Dmytro Sukhovoy

Adding this as an extended version of Marco Demaio answer. Added french days of the week and months abbreviations:

将此添加为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',
      ]
    )
  );
}

回答by Marc B

It's locale dependent. If it had to check every language for every parse, it'd take nigh-on FOREVER to parse even the simplest of date strings.

它取决于语言环境。如果它必须为每个解析检查每种语言,那么即使是最简单的日期字符串也需要几乎永远解析。

If you've got a string with known format, consider using date_create_from_format(), which'll be far more efficient and less error-print

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

回答by Howard

Try to set the locale before conversion:

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

setlocale(LC_TIME, "fr_FR");