感谢Rico Neitzel的提示。与其尝试格式化php日期,不如使用strftime。要使用您的语言查看月份名称的前三个字母(例如,Dezmbro用Dez代替Dec,而不用Dec),请按照上面的语言环境安装说明进行操作,然后:
date命令:date('d M Y') //无法从英文更改
setlocale( LC_ALL, "pt_BR");
echo strftime('%e %b %G');
result: "4 Dez 2016"
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){
$nc = $dict[$c];
if( $c === 'n'){
$nc = preg_replace("/^0+/", '', strftime( $nc));
}
elseif( $c === 'z'){
$nc = preg_replace("/^0+/", '', strftime( $nc));
$nc = intval($nc) - 1;
}
$nstr .= $nc;
}
}
return strftime( $nstr);
}else{
return date( $str, $time);
}
}
setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
?