感谢 Rico Neitzel 的提示。与其尝试格式化 php 日期,不如使用 strftime。要查看您的语言中月份名称的前 3 个字母(例如 Dez 而不是来自 Dezembro 的 Dec 而不是十二月),请按照上面的语言环境安装说明进行操作,然后:
date command: date('d M Y') // 不可能从英文改成
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);
}
}