A
A
AlexKuznec2016-09-08 16:55:11
Yii
AlexKuznec, 2016-09-08 16:55:11

List of timezones for php?

I needed to display a list of time zones on a web form, and suddenly I ran into a lack of a solution out of the box ...
Ideally, I would like to see a localized list indicating offsets from UTC, as in operating systems. For example, my Windows 10 shows time zones like this:
(UTC +7:00) Tomsk
But, rummaging through the internet, I found that people, to form more banal lists, use a bunch of code to get something like (UTC +7) Asia /Tomsk
I also found these dances with tambourines:
torunar.tk/post/110241942071/php-timezone
UPD: Based on it, I created my own version of the localized list using php.
Question: are there ready-made solutions for php or yii2 that can display this banal list by locale?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
AlexKuznec, 2016-09-11
@AlexKuznec

I collected the necessary code and got the following function, which returns an array for dropDownList() from Yii2 with elements of the form:
'Asia/Krasnoyarsk' => '(UTC+07:00) Krasnoyarsk'
An array with two-dimensional sorting, first by shift, then by name.
Localization is still rather weak, maybe that's why the IntlTimeZone class is almost not documented yet.
By the way, the UTC time zone is set inside the function to calculate the time shift, I don't know its scope. So use with caution. In general, I do not like the part with the calculation of this shift. If anyone knows a better way - write in the comments.

/* robust list of timezones */
function get_list_of_timezones($locale) {

    date_default_timezone_set('UTC');

    $identifiers = DateTimeZone::listIdentifiers();
    foreach($identifiers as $i) {
        // create date time zone from identifier
        $dtz = new DateTimeZone($i);
        // create timezone from identifier
        $tz = IntlTimeZone::createTimeZone($i);
        // if IntlTimeZone is unaware of timezone ID, use identifier as name, else use localized name
        if ($tz->getID() === 'Etc/Unknown' or $i === 'UTC') $name = $i;
        else $name =  $tz->getDisplayName(false, 3, $locale);
        // time offset
        $offset = $dtz->getOffset(new DateTime());
        $sign   = ($offset < 0) ? '-' : '+';

        $tzs[] = [
            'code'   => $i,
            'name'   => '(UTC' . $sign . date('H:i', abs($offset)) . ') ' . $name,
            'offset' => $offset,
        ];
    }

    \yii\helpers\ArrayHelper::multisort($tzs, ['offset', 'name']);

    // sort by offset
//    usort($tzs, function($a, $b){
//        if ($a['offset'] > $b['offset']) {
//            return 1;
//        }
//        elseif ($a['offset'] < $b['offset']) {
//            return -1;
//        }
//        elseif ($a['name'] > $b['name']) {
//            return 1;
//        }
//        elseif ($a['name'] < $b['name']) {
//            return -1;
//        }
//        return 0;
//    });

    return array_column($tzs, 'name', 'code');
}
?>

E
entermix, 2016-09-08
@entermix

php.net/manual/ru/timezones.php
https://github.com/tamaspap/timezones

A
Alexander Aksentiev, 2016-09-08
@Sanasol

suddenly faced with no solution out of the box

And now suddenly it appeared xD
php.net/manual/ru/function.timezone-identifiers-li...

S
Sergey Novikov, 2016-09-08
@BOOMER_74

The list of all zones is DateTimeZone::listIdentifiers , plus the time libraries Carbon and Date . Unfortunately, I don’t know anything about libraries with translated zones, so you need to either look further or translate it yourself (I think there are quite a lot of solutions in JS).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question