获取PHP ICU中的日历,时区,语言环境列表(intlDateFormatter)

时间:2015-09-12 15:29:30

标签: localization calendar timezone locale icu

如何在PHP ICU(intlDateFormatter)中获取受支持的日历,时区,语言环境列表?

2 个答案:

答案 0 :(得分:0)

列出时区:

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

另请参阅相关问题,例如Generating a drop down list of timezones with PHP

日历-您的意思是根据日历类型和时区格式化日期-请参见https://www.php.net/manual/en/intldateformatter.create.phphttps://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(版本64.1)属性转储器(600+): https://intl.rmcreative.ru/tables?locale=en

ICU日期格式: http://userguide.icu-project.org/formatparse/datetime

答案 1 :(得分:0)

如何使用 PHP intl 模块创建语言环境、日历和时区数组

要为所有语言环境创建资源包:

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

要列出包中的资源名称:

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

产生:

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

获取所有语言环境的数组:

$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

获取所有日历名称的数组:

$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

要列出特定语言环境的日历,请创建如下包:

$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

要获取未弃用的时区列表并从熟悉的区域(如“亚洲”)开始,请执行以下循环:

// 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

要列出特定国家/地区的时区,请创建如下迭代器:

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

请注意,生成所有列表的时间不到 20 毫秒,包括原始和过滤后的时区列表。