Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

timezone

php

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:

'PST', 'Pacific Standard Time', 'PDT', 'Pacific Daylight Time'

If I could get only the short abbreviation ('PST' and 'PDT'), that would be ok.

I've looked at DateTimeZone::listAbbreviations(), and tried inspecting that to see which correspond with my id, however for America/Los_Angeles, it finds "PST", "PDT", "PPT" and "PWT" which is slightly curious.

like image 284
nickf Avatar asked Mar 19 '11 14:03

nickf


People also ask

How can I get timezone in PHP?

The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.

How do you label time zones?

Reference to a specific time and zone would follow standard guidelines with the zone in parentheses: 4:42 p.m. (PST), 11:03 a.m. (MDT), 2:30 p.m. (CST), 10:00 P.M. (EST). AP on the other hand advises to capitalize the full name of each time zone: Pacific/Mountain/Central/Eastern Standard Time.

What timezone is UTC for PHP?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts. See PHP's list of supported timezones to find the names of all possible timezones you can use for the date.


4 Answers

hope this will help you

<?php
    date_default_timezone_set('Europe/Sofia');
    echo date_default_timezone_get(); // Europe/Sofia
    echo ' => '.date('T'); // => EET
?>
like image 60
Teneff Avatar answered Sep 21 '22 15:09

Teneff


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

like image 33
Joe L. Avatar answered Sep 21 '22 15:09

Joe L.


Hope this will help you

<?php
$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone('America/Havana'));
echo $dateTime->format('T');
?>
like image 43
ajay p Avatar answered Sep 21 '22 15:09

ajay p


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
like image 33
Timo Huovinen Avatar answered Sep 19 '22 15:09

Timo Huovinen