DateTimeZone::listIdentifiers

timezone_identifiers_list

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

DateTimeZone::listIdentifiers -- timezone_identifiers_list返回包含了所有时区标识符的数字索引数组

说明

面向对象风格

public static DateTimeZone::listIdentifiers(int $timezoneGroup = DateTimeZone::ALL, ?string $countryCode = null): array

过程化风格

timezone_identifiers_list(int $timezoneGroup = DateTimeZone::ALL, ?string $countryCode = null): array

参数

timezoneGroup

DateTimeZone 类中的常量之一(或组合)。

countryCode

国家代码,兼容 ISO 3166-1 的两个大写字母。

注意: 只有当 timezoneGroup 设置为 DateTimeZone::PER_COUNTRY 时,该选项才会被使用。

返回值

返回时区标识符的数组。只会返回没有过时的元素。要获取包含过时时区标识符的所有元素,使用 DateTimeZone::ALL_WITH_BC 作为 timezoneGroup 的值。

更新日志

版本 说明
8.0.0 在此版本之前,失败时返回 false
7.1.0 现在,countryCode 可以为 null。

范例

示例 #1 timezone_identifiers_list() 范例

<?php
$timezone_identifiers 
DateTimeZone::listIdentifiers();
for (
$i=0$i 5$i++) {
    echo 
"$timezone_identifiers[$i]\n";
}
?>

以上例程的输出类似于:

Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara

示例 #2 列出指定区域的标识符

<?php
$timezone_identifiers 
DateTimeZone::listIdentifiersDateTimeZone::ASIA );
for (
$i=0$i 5$i++) {
    echo 
"$timezone_identifiers[$i]\n";
}
?>

以上例程的输出类似于:

Asia/Aden
Asia/Almaty
Asia/Amman
Asia/Anadyr
Asia/Aqtau

示例 #3 列出多个区域的标识符

<?php
$timezone_identifiers 
DateTimeZone::listIdentifiersDateTimeZone::ASIA DateTimeZone::PACIFIC );
echo 
join', '$timezone_identifiers );
?>

以上例程的输出类似于:

Asia/Aden, Asia/Almaty, Asia/Amman, Asia/Anadyr, Asia/Aqtau, Asia/Aqtobe,
Asia/Ashgabat, Asia/Atyrau, Asia/Baghdad, Asia/Bahrain, Asia/Baku,
Asia/Bangkok, Asia/Barnaul, Asia/Beirut, Asia/Bishkek, Asia/Brunei,
Asia/Chita, Asia/Choibalsan, Asia/Colombo, Asia/Damascus, Asia/Dhaka,
Asia/Dili, Asia/Dubai, Asia/Dushanbe, Asia/Famagusta, Asia/Gaza, Asia/Hebron,
Asia/Ho_Chi_Minh, Asia/Hong_Kong, Asia/Hovd, Asia/Irkutsk, Asia/Jakarta,
Asia/Jayapura, Asia/Jerusalem, Asia/Kabul, Asia/Kamchatka, Asia/Karachi,
Asia/Kathmandu, Asia/Khandyga, Asia/Kolkata, Asia/Krasnoyarsk,
Asia/Kuala_Lumpur, Asia/Kuching, Asia/Kuwait, Asia/Macau, Asia/Magadan,
Asia/Makassar, Asia/Manila, Asia/Muscat, Asia/Nicosia, Asia/Novokuznetsk,
Asia/Novosibirsk, Asia/Omsk, Asia/Oral, Asia/Phnom_Penh, Asia/Pontianak,
Asia/Pyongyang, Asia/Qatar, Asia/Qostanay, Asia/Qyzylorda, Asia/Riyadh,
Asia/Sakhalin, Asia/Samarkand, Asia/Seoul, Asia/Shanghai, Asia/Singapore,
Asia/Srednekolymsk, Asia/Taipei, Asia/Tashkent, Asia/Tbilisi, Asia/Tehran,
Asia/Thimphu, Asia/Tokyo, Asia/Tomsk, Asia/Ulaanbaatar, Asia/Urumqi,
Asia/Ust-Nera, Asia/Vientiane, Asia/Vladivostok, Asia/Yakutsk, Asia/Yangon,
Asia/Yekaterinburg, Asia/Yerevan, Pacific/Apia, Pacific/Auckland,
Pacific/Bougainville, Pacific/Chatham, Pacific/Chuuk, Pacific/Easter,
Pacific/Efate, Pacific/Fakaofo, Pacific/Fiji, Pacific/Funafuti,
Pacific/Galapagos, Pacific/Gambier, Pacific/Guadalcanal, Pacific/Guam,
Pacific/Honolulu, Pacific/Kanton, Pacific/Kiritimati, Pacific/Kosrae,
Pacific/Kwajalein, Pacific/Majuro, Pacific/Marquesas, Pacific/Midway,
Pacific/Nauru, Pacific/Niue, Pacific/Norfolk, Pacific/Noumea,
Pacific/Pago_Pago, Pacific/Palau, Pacific/Pitcairn, Pacific/Pohnpei,
Pacific/Port_Moresby, Pacific/Rarotonga, Pacific/Saipan, Pacific/Tahiti,
Pacific/Tarawa, Pacific/Tongatapu, Pacific/Wake, Pacific/Wallis

示例 #4 列出单个国家的标识符

<?php
$timezone_identifiers 
DateTimeZone::listIdentifiersDateTimeZone::PER_COUNTRY"UA" );
foreach( 
$timezone_identifiers as $identifier ) {
    echo 
"$identifier\n";
}
?>

以上例程的输出类似于:

Europe/Kyiv
Europe/Simferopol
Europe/Uzhgorod
Europe/Zaporozhye

参见

add a note add a note

User Contributed Notes 3 notes

up
19
kalle at example dot com
6 years ago
Even though the manual currently says that the first parameter has to be "One of DateTimeZone class constants", you may actually combine these constants:

<?php
  $a
= DateTimeZone::listIdentifiers(DateTimeZone::AFRICA); //gives africa time zones
 
$b = DateTimeZone::listIdentifiers(DateTimeZone::AMERICA); //gives american time zones
 
$c = DateTimeZone::listIdentifiers(DateTimeZone::AFRICA | DateTimeZone::AMERICA); //gives both african and american time zones
?>

Be sure to use |, not ||.
up
1
repohelga at example dot com
3 years ago
Beware that the ISO 3166-1 country code passed to second parameter $country has to be in capital letters (eg. 'US', 'DE', ...).
Passing the country code in lower case like 'us' or 'de' will not return the failure-value false but just an empty array.
up
0
PHP Guru
2 years ago
In tests that I have done, not all time zones are returned by this function. For example the following aliases Asia/Katmandu and Asia/Calcutta are not returned, but these time zones are supported in tests that I have done such as the following:

<?php
echo date_default_timezone_set('Asia/Calcutta');
?>

result:

Fri, 19 Jun 2020 03:26:52 +0530

UTC+05:30 is the correct time zone for Calcutta.

Asia/Katmandu and Asia/Calcutta are aliases for Asia/Kathmandu and Asia/Kolkata respectively (which can be found in the list)

Hopefully this helps some people because without these time zones appearing in the list one might not know that they are supported or even exist.
To Top