Penggunaan fungsi US.TIME.ZONES pada PHP

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:

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.

asked Feb 14, 2011 at 5:48

6

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

answered Feb 14, 2011 at 6:01

JohnJohn

9,40025 gold badges89 silver badges136 bronze badges

6

As deceze said, you can use the PER_COUNTRY flag.

$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

answered Sep 10, 2012 at 20:31

jgrowljgrowl

2,0272 gold badges17 silver badges23 bronze badges

1

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.

answered Feb 14, 2011 at 5:55

decezedeceze

497k81 gold badges718 silver badges865 bronze badges

1

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.

$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);

answered Feb 13, 2014 at 8:50

maryomaryo

6,3893 gold badges15 silver badges9 bronze badges