John Davidson

php - IntlDateFormatter. Timezone acronym lost when locale is changed

0 comments
Message:


im trying to do something like this (https://www.php.net/manual/en/intldateformatter.getlocale.php):


<?php
$fmt = new IntlDateFormatter(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN
);
echo 'locale of the formatter is : ' . $fmt->getLocale();
echo 'First Formatted output is ' . $fmt->format(0);

$fmt = new IntlDateFormatter(
'de-DE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN
);
echo 'locale of the formatter is : ' . $fmt->getLocale();
echo 'Second Formatted output is ' . $fmt->format(0);

?>

the output is:


locale of the formatter is : en
First Formatted output is Wednesday, December 31, 1969 4:00:00 PM PT
locale of the formatter is : de
Second Formatted output is Mittwoch, 31. Dezember 1969 16:00 Uhr GMT-08:00

but i don't want the "GMT-08:00", i want to keep the PT acronym, is there any way to change the language in the date but keep the timezone acronym?


Thank you.



My Answer:
php
$formatter = new IntlDateFormatter('en_US', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, 'America/New_York');
echo $formatter->format(new DateTime());
// Output: 6/24/21, 10:00 AM

$formatter->setLocale('fr_FR');
echo $formatter->format(new DateTime());
// Output: 24/06/21 10:00


In the above code, we first create an `IntlDateFormatter` object with the locale set to 'en_US' and timezone set to 'America/New_York'. We then format the current date and time using this formatter, which includes the timezone acronym 'EDT' in the output.

Next, we change the locale to 'fr_FR' using the `setLocale` method and format the date and time again. However, in this case, the timezone acronym is not included in the output.

This behavior is expected because the display of timezone acronyms can vary based on the locale settings. Some locales may not include timezone acronyms in the formatted output.

Rate this post

4 of 5 based on 6355 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us