Get all timezones

时间:2017-08-05 11:16:34

标签: php datetime timezone

I am trying to get all the timezones. What I have done is:

$tzlist = DateTimeZone::listIdentifiers(DateTimeZone::ALL);

It gives me an array like this:

Array
(
    [0] => Africa/Abidjan
    [1] => Africa/Accra
    [2] => Africa/Addis_Ababa
    [3] => Africa/Algiers
    [4] => Africa/Asmara
    [5] => Africa/Bamako
    [6] => Africa/Bangui
    [7] => Africa/Banjul
    [8] => Africa/Bissau....

I want to change the timezone name to full timezone name for all timezones e.g.

Australia/Darwin = AUS Central Standard Time

You can see the example here

2 个答案:

答案 0 :(得分:4)

You can use IntlTimeZone::getDisplayName(), e.g.:

echo IntlTimeZone::createTimeZone("Australia/Darwin")->getDisplayName();

Outputs:

Australian Central Standard Time

Though many will change throughout the year for daylight saving. If you want to check for this and add the alternate daylight/summer display name you can do something like:

$tz = IntlTimeZone::createTimeZone("America/Los_Angeles");
echo $tz->getDisplayName(), "\n";
if ($tz->useDaylightTime()) {
    echo $tz->getDisplayName(true);
}

which outputs:

Pacific Standard Time
Pacific Daylight Time

答案 1 :(得分:0)

What you are asking are IDs of Microsoft Windows time zones (here). PHP uses IANA/Olson time zones. See the timezone tag wiki for details.

From PHP official documentation, you can try this:

<?php

$timezones = DateTimeZone::listAbbreviations();

$cities = array();
foreach( $timezones as $key => $zones )
{
    foreach( $zones as $id => $zone )
    {
        /**
         * Only get timezones explicitely not part of "Others".
         * @see http://www.php.net/manual/en/timezones.others.php
         */
        if ( preg_match( '/^(America|Antartica|Arctic|Asia|Atlantic|Europe|Indian|Pacific)\//', $zone['timezone_id'] ) )
            $cities[$zone['timezone_id']][] = $key;
    }
}

// For each city, have a comma separated list of all possible timezones for that city.
foreach( $cities as $key => $value )
    $cities[$key] = join( ', ', $value);

// Only keep one city (the first and also most important) for each set of possibilities. 
$cities = array_unique( $cities );

// Sort by area/city name.
ksort( $cities );

?>

Here another SO question that you can look, there are lots of similar answers: Generating a drop down list of timezones with PHP

If none of this helps, you can try the above code, to convert the data to your timezone. For example let us assume we have a UTC date and time string (2017-08-05 02:45) that we would like to convert to ACST (Australian Central Standard Time).

<?php
$utc_date = DateTime::createFromFormat(
    'Y-m-d G:i',
    '2017-08-05 02:45',
    new DateTimeZone('UTC')
);

$acst_date = clone $utc_date; // we don't want PHP's default pass object by reference here
$acst_date->setTimeZone(new DateTimeZone('Australia/Yancowinna'));

echo 'UTC:  ' . $utc_date->format('Y-m-d g:i A');  // UTC:  2017-08-05 02:45 AM
echo 'ACST: ' . $acst_date->format('Y-m-d g:i A'); // ACST: 2017-08-05  14:15 PM

Hope it helps!

UPDATE:

From here, you have a Windows ids timezone to PHP:

AUS Central Standard Time,1,Australia/Darwin AUS Central Standard Time,AU,Australia/Darwin AUS Eastern Standard Time,1,Australia/Sydney AUS Eastern Standard Time,AU,Australia/Melbourne AUS Eastern Standard Time,AU,Australia/Sydney

change to a php array:

$timezonesArray = 
['AUS Central Standard Time','1','Australia/Darwin',
'AUS Central Standard Time','AU','Australia/Darwin',
'AUS Eastern Standard Time','1','Australia/Sydney',
'AUS Eastern Standard Time','AU','Australia/Melbourne',
'AUS Eastern Standard Time','AU','Australia/Sydney',
'Afghanistan Standard Time','1','Asia/Kabul',
'Afghanistan Standard Time','AF','Asia/Kabul',,
'Alaskan Standard Time','1','America/Anchorage',
'Alaskan Standard Time','US','America/Anchorage',
'Alaskan Standard Time','US','America/Juneau',
'Alaskan Standard Time','US','America/Nome',
'Alaskan Standard Time','US','America/Sitka',
'Alaskan Standard Time','US','America/Yakutat']; 
//... the array continues

and then you can make a function to translate your timezone (array position 2) with your desired windows id (array position[0]), with find or whatever you want.

It's a not the more elegant solution I guess but it will work and its simple. You can search over the array and return the required translation from one codification to the other.

Hope it helps now, happy coding!