获取 PHP ICU 中的日历、时区、区域设置列表 (intlDateFormatter) [英] Get list of calendars, timezones, locales in PHP ICU (intlDateFormatter)
问题描述
如何在 PHP ICU (intlDateFormatter) 中获取支持的日历、时区、区域设置列表?
How can I get list of supported calendars, timezones, locales in PHP ICU (intlDateFormatter)?
推荐答案
如何使用 PHP intl 模块创建语言环境、日历和时区数组
要为所有语言环境创建资源包:
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;}
产生:
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('');
产生:
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;}
产生:
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');
要列出所有时区:
// 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();
}
生产:
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();
}
生产:
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
...
Australia/Perth
Australia/Sydney
Etc/UTC
Europe/Amsterdam
Europe/Andorra
...
Pacific/Truk
Pacific/Wake
Pacific/Wallis
请注意,它包含了 UTC
的规范名称作为 Etc/UTC
.
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');
请注意,生成所有列表的时间不到 20 毫秒,包括原始和过滤后的时区列表.
Note that it took less than 20ms to generate all lists, including the raw and filtered timezone lists.
这篇关于获取 PHP ICU 中的日历、时区、区域设置列表 (intlDateFormatter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!