Nhờ Rico Neitzel cho gợi ý. Thay vì cố gắng định dạng ngày tháng php, hãy sử dụng strftime. Để xem 3 chữ cái đầu tiên của tên tháng trong ngôn ngữ của bạn (Ví dụ: Dez thay vì tháng 12 từ Dezembro và không phải tháng 12), hãy làm theo hướng dẫn cài đặt ngôn ngữ địa phương ở trên, và sau đó:
date date: 'd M Y ') // không thể thay đổi từ tiếng Anh
setlocale(LC_ALL, "pt_BR"); // Portuguese, replace with your locale
echo strftime('%e %b %G');
result: "4 Dez 2016"
/**
* datelo funcion (date with locale)
* Credits: Sergio Abreu
* http://sites.sitesbr.net
* NOTE: Depend on availability of the locale in server.
*
*/
function datelo($str, $locale='en_US', $time=null){
if($time === null){ $time = time(); }
if (preg_match("/[DlFM]/", $str)){
setlocale(LC_ALL, $locale);
$dict = array('d'=>'%d', 'D'=>'%a', 'j'=>'%e', 'l'=>'%A', 'N'=>'%u', 'w'=>'%w', 'F'=>'%B',
'm'=>'%m', 'M'=>'%b', 'Y'=>'%G', 'g'=>'%l', 'G'=>'%k', 'h'=>'%I', 'H'=>'%H', 'i'=>'%M',
's'=>'%S', 'S'=>'', 'z'=>'%j', 'n'=>'%m', ' '=>' ', '-'=>'-', '/'=>'/', ':'=>':', ','=>',');
$chars = preg_split("//", $str);
$nstr = '';
foreach ($chars as $c){
if ($c){ //skip empties
$nc = $dict[$c];
if($c === 'n'){ // Fixes the extra zero
$nc = preg_replace("/^0+/", '', strftime($nc));
}
elseif($c === 'z'){ // Fixes the extra zero and decrease 1
$nc = preg_replace("/^0+/", '', strftime($nc)); // 023 turns 23
$nc = intval($nc) - 1;
}
$nstr .= $nc;
}
}
return strftime($nstr);
}else{ // not localized
return date($str, $time);
}
}
Vì vậy, bạn đã thử các phiên bản khác của ngôn ngữ Đức, ví dụ: 'setlocale (LC_ALL, 'de_DE @ euro', 'de_DE', 'de', 'ge');'? –
Bạn nên kiểm tra xem ngôn ngữ có thay đổi thành công hay không bằng cách kiểm tra giá trị trả về của 'setLocale' – velop