Get list of calendars, timezones, locales in PHP ICU (intlDateFormatter)
Asked Answered
M

2

1

How can I get list of supported calendars, timezones, locales in PHP ICU (intlDateFormatter)?

Mcquiston answered 12/9, 2015 at 15:29 Comment(2)
Have you tried something? Pls show us some parts of your code. ThanksGrewitz
@arman1991, I just want to get a list of these items to add localization features to my web app so each user can see dates and times in his/her preferred calendar, language and timezone. In other words, I'm looking for functions that returns a list of calendars like Gregorian, Persian, Hebrew, Buddhist, Islamic, etc that is supported by current PHP ICU installation as well as functions for timezones and locales.Mcquiston
G
1

How to use the PHP intl module to create arrays of locales, calendars and timezones

To create a resource bundle for all locales:

$bundle=new ResourceBundle('','ICUDATA');

To list the resource names in the bundle:

$rnames=[];
foreach($bundle as $n=>$v){$rnames[]=$n;}

which produces:

AuxExemplarCharacters
Ellipsis
ExemplarCharacters
ExemplarCharactersNumbers
ExemplarCharactersPunctuation
MoreInformation
NumberElements
Version
calendar
characterLabel
delimiters
fields
layout
listPattern
measurementSystemNames
parse

To get an array of all locales:

$locales=$bundle->getLocales('');

which produces:

af
af_NA
af_ZA
agq
agq_CM
ak
ak_GH
...
gv_IM
ha
ha_GH
ha_NE
ha_NG
haw
...
zh_Hant
zh_Hant_HK
zh_Hant_MO
zh_Hant_TW
zu
zu_ZA

To get an array of all calendar names:

$cnames=[];
$calendars=$bundle->get('calendar');
foreach($calendars as $n=>$v){$cnames[]=$n;}

which produces:

buddhist
chinese
coptic
dangi
default
ethiopic
ethiopic-amete-alem
generic
gregorian
hebrew
indian
islamic
islamic-civil
islamic-rgsa
islamic-tbla
islamic-umalqura
japanese
persian
roc

To list the calendars for a particular locale, create the bundle like:

$bundle=new ResourceBundle('en','ICUDATA');

To list all timezones:

// CREATE TIMEZONE ITERATOR AND SET TO START
$zones=[];
$zone_iter=IntlTimeZone::createEnumeration(NULL);
$zone_iter->rewind();

// WHILE VALID TIMEZONE
while($zone_iter->valid()){
 // ADD TIMEZONE TO ARRAY
 $zones[]=$zone_iter->current();

 // NEXT TIMEZONE
 $zone_iter->next();
}

to produce:

ACT
AET
AGT
ART
AST
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
...
Atlantic/South_Georgia
Atlantic/St_Helena
Atlantic/Stanley
Australia/ACT
Australia/Adelaide
Australia/Brisbane
...
US/Pacific
US/Pacific-New
US/Samoa
UTC
Universal
VST
W-SU
WET
Zulu

To get a list of timezones that are not deprecated and start with the familiar regions like 'Asia', make the loop:

// WHILE VALID TIMEZONE
while($zone_iter->valid()){
 // IF NOT DEPRECATED AND STARTS WITH FAMILIAR REGION NAME
 $zone=$zone_iter->current();
 $tzone=intltz_create_time_zone($zone);
 $czone=$tzone->getCanonicalID($zone);
 if(($zone==$cid)&&
    (preg_match('~^(Af|Am|An|As|At|Au|Eu|In|Pa|Etc/UTC)~',$zone)===1)){
  // ADD TIMEZONE TO ARRAY
  $zones[]=$zone;
 }

 // NEXT TIMEZONE
 $zone_iter->next();
}

to produce:

Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
...
Australia/Perth
Australia/Sydney
Etc/UTC
Europe/Amsterdam
Europe/Andorra
...
Pacific/Truk
Pacific/Wake
Pacific/Wallis

Note that it includes the canonical name for UTC as Etc/UTC.

To list the timezones for a particular country, create the iterator like:

$zone_iter=IntlTimeZone::createEnumeration('AU');

Note that it took less than 20ms to generate all lists, including the raw and filtered timezone lists.

Gusman answered 7/8, 2021 at 7:52 Comment(0)
H
-1

List timezones:

$all_time_zones = DateTimeZone::listIdentifiers(DateTimeZone::ALL); // 425 zones

See also related question e.g. Generating a drop down list of timezones with PHP.

Calendar - you mean formatting the date depending on calendar type and timezone - see https://www.php.net/manual/en/intldateformatter.create.php and https://www.php.net/manual/en/intldateformatter.format.php:

// basic example
$formatter = IntlDateFormatter::create('en_US', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Europe/Prague', IntlDateFormatter::GREGORIAN);
$calendar = IntlCalendar::createInstance('Europe/Prague');
$now_en = $formatter->format($calendar); // "Monday, August 26, 2019 at 12:01:47 PM Central European Summer Time"

// localized with date pattern
$formatter = IntlDateFormatter::create('cs_CS', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Europe/Prague', IntlDateFormatter::GREGORIAN, 'dd.MM.yyyy, eeee, H:mm');
$calendar = IntlCalendar::createInstance('Europe/Prague');
$now_cs = $formatter->format($calendar); // "26.08.2019, pondělí, 14:30"

ICU (ver. 64.1) properties dumper (600+): https://intl.rmcreative.ru/tables?locale=en

ICU date formats: http://userguide.icu-project.org/formatparse/datetime

Howes answered 26/8, 2019 at 10:17 Comment(1)
Voted down because none of this answer covers how to LIST (not format) available calendars, timezones and locales USING the intl module.Gusman

© 2022 - 2024 — McMap. All rights reserved.