[英]PHP IntlDateFormatter - remove "day" from the date format
I want to remove "day" from the pattern to display the correct INTL date in format "F, Y".我想从模式中删除“日”,以“F,Y”格式显示正确的 INTL 日期。 How to correctly do it without str_replace-ing "d" character with dots (and other symbols) in the $tempPattern variable?如何在 $tempPattern 变量中没有带点(和其他符号)的 str_replace-ing "d" 字符的情况下正确地做到这一点?
I can't use $formatter->setPattern(), as the date format is different for each language.我不能使用 $formatter->setPattern(),因为每种语言的日期格式都不同。
function getLocaleDateFormate($locale) {
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::NONE);
// replace M with L: stand-alone month in year
$tempPattern = $formatter->getPattern();
$tempPattern = str_replace("M", "L", $tempPattern);
return $tempPattern;
}
echo getLocaleDateFormate("sk"); // d. LLLL y
echo getLocaleDateFormate("hu"); // y. LLLL d.
echo getLocaleDateFormate("de"); // d. LLLL y
echo getLocaleDateFormate("es"); // d 'de' LLLL 'de' y
echo getLocaleDateFormate("pl"); // d LLLL y
echo getLocaleDateFormate("cn"); // y年L月d
The output is not in the correct order (it forces the 'LLLL y' order for each language), so I applied the corrections for specific languages.输出的顺序不正确(它强制每种语言的“LLLL y”顺序),所以我对特定语言应用了更正。 I am not satisfied fully but I don't have any better solution.我并不完全满意,但我没有更好的解决方案。
if ($locale == "hu") // corrections may apply for specific languages
$formatter->setPattern("y. LLLL");
elseif ($locale == "cn")
$formatter->setPattern("y年L月");
else
$formatter->setPattern("LLLL y"); // stand-alone-monthName Year
return $formatter->getPattern();
I ended up creating a function that would format dates locally, hoping to help !我最终创建了一个可以在本地格式化日期的函数,希望能有所帮助!
const LANG_BACK = 'fr';
const TIMEZONE = 'Europe/Paris';
/**
* format_date_locale( $date_Object, $dateType , $timeType, $pattern );
*
* @param {object} $date_Object DateTime object or object accepted
* @param {string} $dateType 'NONE', 'SHORT', 'MEDIUM', 'LONG', 'FULL'
* @param {string} $timeType 'NONE', 'SHORT', 'MEDIUM', 'LONG', 'FULL'
* @param {pattern} $pattern 'MMMM y' -> may 2022
* see how to set patterns: https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax
* @return {string} date formated locally
*/
format_date_locale( $date_Object, $dateType , $timeType, $pattern ){
// date format
switch ( $dateType ) {
case 'NONE':
$Date_Format = IntlDateFormatter::NONE; // 20220606 08:16 AM
break;
case 'SHORT':
$Date_Format = IntlDateFormatter::SHORT; // 06/06/2022
break;
case 'MEDIUM':
$Date_Format = IntlDateFormatter::MEDIUM; // 6 juin 2022 in [fr] must vary
break;
case 'LONG':
$Date_Format = IntlDateFormatter::LONG; // 6 juin 2022
break;
case 'FULL':
$Date_Format = IntlDateFormatter::FULL; // lundi 6 juin 2022
break;
default:
$Date_Format = IntlDateFormatter::SHORT;
break;
}
// time format
switch ( $timeType ) {
case 'NONE':
$Time_Format = IntlDateFormatter::NONE; // ''
break;
case 'SHORT':
$Time_Format = IntlDateFormatter::SHORT; // 08:11
break;
case 'MEDIUM':
$Time_Format = IntlDateFormatter::MEDIUM; // 08:11:10
break;
case 'LONG':
$Time_Format = IntlDateFormatter::LONG; // 08:09:33 UTC+2
break;
case 'FULL':
$Time_Format = IntlDateFormatter::FULL; // 08:10:38 heure d’été d’Europe centrale
break;
default:
$Time_Format = IntlDateFormatter::SHORT;
break;
}
// create date formatter
// LANG_BACK, TIMEZONE, -> const API
$local_date = IntlDateFormatter::create(
LANG_BACK, // lang
$Date_Format, // date
$Time_Format, // time
TIMEZONE, // timezone
null, // type of calendar / null -> const IntlDateFormatter::GREGORIAN
$pattern // pattern to apply or null
);
// return date formatted
return $local_date->format( $date_Object );
}
/**
* end format_date_locale( $date, $dateType , $timeType, $pattern );
*/
Use :利用 :
$date_Object = new DateTime('now', new DateTimeZone(TIMEZONE) );
echo format_date_locale( $date_Object, 'FULL' , 'SHORT', null );
// output : jeudi 9 juin 2022 15:38
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.