Sử dụng strftime()
kết hợp với setlocale()
là một tùy chọn.
Tuy nhiên, bạn nên lưu ý rằng khi cài đặt php luồng, setlocale()
có thể hoạt động không mong muốn vì thông tin ngôn ngữ được duy trì cho mỗi quy trình chứ không phải trên mỗi luồng. Do đó, điều quan trọng là gọi setlocale()
mỗi lần trước mỗi cuộc gọi đến strftime()
để đảm bảo nó sử dụng đúng ngôn ngữ.
Ngoài ra, đối với hệ thống Windows, bạn cần phải sử dụng các chuỗi hơi khác thường cho tham số $locale
cho setlocale()
.
Xem tài liệu để biết thêm thông tin về cả hai vấn đề này.
Something như thế này nên làm việc:
// define the locales for setlocale() for which we need the daynames
$locales = array(
'en_EN',
'de_DE',
'nl_NL'
// etc...
);
// be aware that setlocale() needs different values on Windows machines
// see the docs on setlocale() for more information
$locales = array(
'english',
'german',
'dutch'
// etc...
);
// let's remember the current local setting
$oldLocale = setlocale(LC_TIME, '0');
// initialize out result array
$localizedWeekdays = array();
// loop each locale
foreach($locales as $locale)
{
// create sub result array for this locale
$localizedWeekdays[ $locale ] = array();
// 7 days in a week
for($i = 0; $i < 7; $i++)
{
// set the locale on each iteration again
setlocale(LC_TIME, $locale);
// combine strftime() with the nifty strtotime()
$localizedWeekdays[ $locale ][] = strftime('%A', strtotime('next Monday +' . $i . ' days'));
// reset the locale for other threads, as a courtesy
setlocale(LC_TIME, $oldLocale);
}
}
// there is your result in a multi-dimensional array
var_dump($localizedWeekdays);
Nguồn
2011-10-14 09:48:54
Tên ngày? Ý của bạn có phải là 'Thứ Hai, Thứ Ba, Thứ Tư ... vv?' – LouwHopley
Tại sao không tự định nghĩa chúng? Nó thường là cách tốt nhất để đi vì yêu cầu 'date()' phụ thuộc vào các thiết lập miền địa phương nổi tiếng không đáng tin cậy. –
* (tham khảo) * http://www.php.net/manual/en/intldateformatter.format.php – Gordon