時間服務器是否僅在服務器上以默認語言工作?下面的代碼應該解析爲2005年8月11日,但它使用法語「aout」而不是英文「aug」。strtotime使用不同的語言?
任何想法如何處理?
<?php
$date = strtotime('11 aout 05');
echo date('d M Y',$date);
?>
時間服務器是否僅在服務器上以默認語言工作?下面的代碼應該解析爲2005年8月11日,但它使用法語「aout」而不是英文「aug」。strtotime使用不同的語言?
任何想法如何處理?
<?php
$date = strtotime('11 aout 05');
echo date('d M Y',$date);
?>
從docs
解析任何英文文本的日期時間描述爲Unix 時間戳
嘗試轉換之前設置的語言環境:
setlocale(LC_TIME, "fr_FR");
這是區域依賴。如果它必須檢查每個語言的每一個解析,那麼它就要花費很長時間才能解析即使是最簡單的日期字符串。
如果你有與已知格式的字符串,可以考慮使用date_create_from_format(),which'll可以更加高效和更少的錯誤打印
這是錯誤的。 DateTime不會解析非英文數據。即這將失敗: setlocale(LC_TIME,「fr_FR」); $ dt = DateTime :: createFromFormat('d F Y',「12Août2013」); – Spir
如前所述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).
請注意,根據您的系統,語言環境和編碼,你將不得不考慮重音字符。
從文檔中還要注意:_這個函數沒有在Windows平臺上實現,這個函數可以在不同的操作系統中有不同的行爲,並且你至少需要php 5.1。 – PhoneixS
法國月份日期爲:
維耶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英文,因爲它不會做任何替換。
這個方法,你應該使用strftime
的工作:
setlocale (LC_TIME, "fr_FR.utf8"); //Setting the locale to French with UTF-8
echo strftime(" %d %h %Y",strtotime($date));
我寫了一個簡單的功能,部分解決了這個問題。 它不能用作完整的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;
}
解決這個問題的關鍵是外國文字的格式轉換成他們的英國同行。我也需要這個,因爲已經給出了答案,我寫了一個很好的,乾淨的函數,它可以用來檢索英文月份名稱。
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
您可以調整這一天的星期幾。
將完整的日期字符串傳遞給它後,我發現你可以在同一個函數中進行月份名稱替換和日期名稱替換。 –
添加此作爲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',
]
)
);
}
這應該是公認的答案。 – Spir
它不回答「任何想法如何處理這個問題?」 –
不解決問題 – onassar