供 PHP 使用的美国时区列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4989209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List of US Time Zones for PHP to use?
提问by John
Is there just a list of time zones for the united states I can use with php? Im using the timezone names from php such as America/Anchorage, php lists time zones here:
是否只有美国的时区列表可以与 php 一起使用?我使用 php 中的时区名称,例如 America/Anchorage,php 在这里列出了时区:
http://us.php.net/manual/en/timezones.others.php
http://us.php.net/manual/en/timezones.others.php
I want to display them in a drop down for the user to select but I only need the ones for the US, I dont want to show a bunch that are outside the US.
我想在下拉列表中显示它们供用户选择,但我只需要美国的那些,我不想显示美国以外的一堆。
回答by John
Here is a list I found:
这是我找到的列表:
Eastern Time America/New_York
Central Time America/Chicago
Mountain Time America/Denver
Mountain Time (no DST) America/Phoenix
Pacific Time America/Los_Angeles
Alaska Time America/Anchorage
Hawaii-Aleutian America/Adak
Hawaii-Aleutian Time (no DST) Pacific/Honolulu
回答by jgrowl
As deceze said, you can use the PER_COUNTRY flag.
正如 deceze 所说,您可以使用 PER_COUNTRY 标志。
$timezone_identifiers = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'US');
foreach($timezone_identifiers as $timezone_identifier) {
echo "$timezone_identifier\n";
}
Output:
输出:
America/Adak
America/Anchorage
America/Boise
America/Chicago
America/Denver
America/Detroit
America/Indiana/Indianapolis
America/Indiana/Knox
America/Indiana/Marengo
America/Indiana/Petersburg
America/Indiana/Tell_City
America/Indiana/Vevay
America/Indiana/Vincennes
America/Indiana/Winamac
America/Juneau
America/Kentucky/Louisville
America/Kentucky/Monticello
America/Los_Angeles
America/Menominee
America/Metlakatla
America/New_York
America/Nome
America/North_Dakota/Beulah
America/North_Dakota/Center
America/North_Dakota/New_Salem
America/Phoenix
America/Shiprock
America/Sitka
America/Yakutat
Pacific/Honolulu
回答by deceze
This will automatically get you a list of American timezones:
这将自动为您提供美国时区列表:
if (defined('DateTimeZone::AMERICA')) {
// PHP 5.3+
$timezoneIdentifiers = timezone_identifiers_list(DateTimeZone::AMERICA);
} else {
// PHP 5.2
$timezoneIdentifiers = timezone_identifiers_list();
$timezoneIdentifiers = array_filter($timezoneIdentifiers, create_function('$tz', 'return preg_match("/^America\//", $tz);'));
}
Note that this will include South American, Canadian etc timezones as well though. You could play around with DateTimeZone::PER_COUNTRY
as a filter to timezone_identifiers_list
for PHP 5.3 and see if that gets you want you want, or filter the complete list for US/
. The best choice is probably to handpick the timezones though.
请注意,这也将包括南美、加拿大等时区。您可以DateTimeZone::PER_COUNTRY
将其timezone_identifiers_list
用作 PHP 5.3的过滤器,看看是否符合您的要求,或者过滤US/
. 最好的选择可能是亲自挑选时区。
回答by maryo
Brute-force way to get ALL timezones per country using PHP's GEOIP. The code is not intended to look nice. You should call this only once and store the result somewhere.
使用 PHP 的 GEOIP 获取每个国家/地区的所有时区的蛮力方法。该代码的目的不是为了好看。您应该只调用一次并将结果存储在某处。
$timezones = array();
foreach (range('A', 'Z') as $i) {
foreach (range('A', 'Z') as $j) {
$country = $i . $j;
foreach (range('A', 'Z') as $k) {
foreach (range('A', 'Z') as $l) {
$region = $k . $l;
if ($timezone = geoip_time_zone_by_country_and_region($country, $region)) {
$timezones[$country][$timezone] = true;
}
}
}
foreach (range(0, 99) as $m) {
if ($timezone = geoip_time_zone_by_country_and_region($country, sprintf('%02d', $m))) {
$timezones[$country][$timezone] = true;
}
}
}
}
var_dump($timezones);