How to get the names and abbreviations of a time zone in PHP?

前端 未结 5 1088
再見小時候
再見小時候 2020-12-16 14:31

Starting with a time zone identifier, such as \"America/Los_Angeles\", how do you find the names and abbreviations of that time zone in PHP? For example:

<
相关标签:
5条回答
  • 2020-12-16 14:45

    I hope this helps:

    function get_timezone_abbreviation($timezone_id)
    {
        if($timezone_id){
            $abb_list = timezone_abbreviations_list();
    
            $abb_array = array();
            foreach ($abb_list as $abb_key => $abb_val) {
                foreach ($abb_val as $key => $value) {
                    $value['abb'] = $abb_key;
                    array_push($abb_array, $value);
                }
            }
    
            foreach ($abb_array as $key => $value) {
                if($value['timezone_id'] == $timezone_id){
                    return strtoupper($value['abb']);
                }
            }
        }
        return FALSE;
    }
    

    get_timezone_abbreviation('America/New_York');

    And you get:

    EDT

    0 讨论(0)
  • 2020-12-16 14:46

    The thing is, a timezone name depends on the time of the year, for example in the winter it's CET, in the summer it's CEST.

    We can get the name of the timezone by using the current date and time.

    $timezone = 'Pacific/Midway';
    $dt = new DateTime('now', new DateTimeZone($timezone));
    $abbreviation = $dt->format('T'); // SST
    

    it only supports the timezones that php knows, it didn't know what "Pacific Standard Time" was.

    Here you can see how it switches between CET and CEST

        $t = new CDateTime('2015-09-22 11:00', new DateTimeZone('CET'));
        $t->format('T'); // CEST
    
        $t = new CDateTime('2015-12-22 11:00', new DateTimeZone('CET'));
        $t->format('T'); // CET
    
    0 讨论(0)
  • 2020-12-16 15:03

    hope this will help you

    <?php
        date_default_timezone_set('Europe/Sofia');
        echo date_default_timezone_get(); // Europe/Sofia
        echo ' => '.date('T'); // => EET
    ?>
    
    0 讨论(0)
  • 2020-12-16 15:03

    Looks like Symphony has methods for that, e.g. select_timezone_tag. You might check their source code to see how it's done.

    0 讨论(0)
  • 2020-12-16 15:06

    Hope this will help you

    <?php
    $dateTime = new DateTime();
    $dateTime->setTimeZone(new DateTimeZone('America/Havana'));
    echo $dateTime->format('T');
    ?>
    
    0 讨论(0)
提交回复
热议问题