Reputation: 151
I want to remove "day" from the pattern to display the correct INTL date in format "F, Y". How to correctly do it without str_replace-ing "d" character with dots (and other symbols) in the $tempPattern variable?
I can't use $formatter->setPattern(), as the date format is different for each language.
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
Upvotes: 1
Views: 1772
Reputation: 587
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
Upvotes: 0
Reputation: 151
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. 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();
Upvotes: 1