当升级到PHP8.1时,我发现了一个关于"strftime“的错误。如何更正代码以以任何语言正确显示整个月的名称?
$date = strftime("%e %B %Y", strtotime('2010-01-08'))发布于 2022-04-08 16:15:21
我选择使用bc/strftime composer包作为替代。
这里是文档。
请注意,输出可能与本机strftime的不同,因为php81_bc/strftime使用不同的库进行区域设置感知格式(ICU)。
注意,libc和这个函数之间的输出可能略有不同,因为它使用的是ICU。
发布于 2022-04-07 03:04:08
敬我亲爱的晚辈().我找到了一种方法来适应IntlDateFormatter::formatObject,下面是对模式的引用的链接:
..。对于那些想更精确地格式化日期的人来说
// "date_default_timezone_set" may be required by your server
date_default_timezone_set( 'Europe/Paris' );
// make a DateTime object
// the "now" parameter is for get the current date,
// but that work with a date recived from a database
// ex. replace "now" by '2022-04-04 05:05:05'
$dateTimeObj = new DateTime('now', new DateTimeZone('Europe/Paris'));
// format the date according to your preferences
// the 3 params are [ DateTime object, ICU date scheme, string locale ]
$dateFormatted =
IntlDateFormatter::formatObject(
$dateTimeObj,
'eee d MMMM y à HH:mm',
'fr'
);
// test :
echo ucwords($dateFormatted);
// output : Jeu. 7 Avril 2022 à 04:56发布于 2022-01-31 18:43:58
您可以使用IntlDateFormatter类。类独立于区域设置工作。有这样的功能
function formatLanguage(DateTime $dt,string $format,string $language = 'en') : string {
$curTz = $dt->getTimezone();
if($curTz->getName() === 'Z'){
//INTL don't know Z
$curTz = new DateTimeZone('UTC');
}
$formatPattern = strtr($format,array(
'D' => '{#1}',
'l' => '{#2}',
'M' => '{#3}',
'F' => '{#4}',
));
$strDate = $dt->format($formatPattern);
$regEx = '~\{#\d\}~';
while(preg_match($regEx,$strDate,$match)) {
$IntlFormat = strtr($match[0],array(
'{#1}' => 'E',
'{#2}' => 'EEEE',
'{#3}' => 'MMM',
'{#4}' => 'MMMM',
));
$fmt = datefmt_create( $language ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
$curTz, IntlDateFormatter::GREGORIAN, $IntlFormat);
$replace = $fmt ? datefmt_format( $fmt ,$dt) : "???";
$strDate = str_replace($match[0], $replace, $strDate);
}
return $strDate;
}您可以使用格式参数,如日期时间。
$dt = date_create('2022-01-31');
echo formatLanguage($dt, 'd F Y','pl'); //31 stycznia 2022有一些用于DateTime的扩展类,其功能集成为方法。
echo dt::create('2022-01-31')->formatL('d F Y','pl');https://stackoverflow.com/questions/70930824
复制相似问题