1

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

Pradeep Singh
  • 1,282
  • 2
  • 19
  • 35
  • Please comment who are downvoting the question so i can improve the same – Pradeep Singh Aug 05 '17 at 11:27
  • 1
    I didn't downvote, but what is the question/problem? – Andreas Aug 05 '17 at 11:29
  • @Andreas I want time zone like `Australia/Darwin` full name as `AUS Central Standard Time` for all the timezones – Pradeep Singh Aug 05 '17 at 11:30
  • 1
    I think that the downvote is because you don't explain your exact problem, only "what you want". What you tried? What's your code after the $tzlist? Did you want to select `Australia/Darwin` in the array or do you want to change `Australia/Darwin` to `AUS Central...`? Try to post your exact problem and what you have searched / tried (and the code!) – JP. Aulet Aug 05 '17 at 11:32
  • @JP.Aulet I think now the question is clear, But i didn't find any useful code to post with the question – Pradeep Singh Aug 05 '17 at 11:45
  • The problem is that the full name for a lot of timezones derived using the `IntlTimeZone` object in the PHP `intl` module is `GMT` with an offset, which for a page with worldwide usage would be very cryptic for many users. At least with the official names, they have a city name that they likely know is in their time zone. Also, the time zone is the current one, which may be summer time. – Patanjali Aug 13 '21 at 03:50

2 Answers2

5

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
user3942918
  • 25,539
  • 11
  • 55
  • 67
  • Thats the answer I was looking for :) – Pradeep Singh Aug 05 '17 at 12:21
  • 1
    @PradeepSingh - are you sure? In [this comment](https://stackoverflow.com/questions/45521276/get-all-timezones#comment78003673_45521624) you confirmed you wanted windows IDs. That is different than these locale-specific display names (which come from CLDR). Which is it?? – Matt Johnson-Pint Aug 06 '17 at 22:01
  • @MattJohnson Yes, I want the same but I was not confirm what to name that, when I found that windows Ids looks same as I want I asked for that – Pradeep Singh Aug 07 '17 at 06:07
  • Note that `getDisplayName` produces `GMT` followed by an offset for many canonical (non-deprecated) time zones, which may be more confusing than the ID for users in some time zones. – Patanjali Aug 13 '21 at 04:07
1

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!

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39
  • Yes @JP.Aulet, I want IDs of windows time zone, so I can get them in a array like `Australia/Darwin => AUS Central Standard Time Australia/Melbourne => AUS Eastern Standard Time Australia/Sydney => AUS Eastern Standard Time Asia/Kabul => Afghanistan Standard Time America/Anchorage => Alaskan Standard Time` – Pradeep Singh Aug 05 '17 at 12:07