A head scratcher for you.
I am grabbing geo IP data from IPInfoDB's API and it returns a timezone offset from UTC including DST (if currently reflected).
For example, I live in EST (-5) and currently it's DST, so the geo IP API returns (-04:00) as the offset.
This is wonderful since DST is a freaking headache. But to my surprise, it caused another headache.
I load this data in PHP to be passed via AJAX to the application. I would like to have the live local time of the IP address on the app.
I have that all set perfectly, but I am going crazy trying to figure out how to set the PHP timezone to match the offset so I can just grab the current hours date('H'); and minutes date('i'); to pass to via AJAX.
I am unsure if there is a specific function that can just give me the current hours and minutes based on that offset or if there is a practical way to set the timezone based on the offset (which will have DST already applied if is in effect).
I've been searching and searching Google to find an answer to this, but what I am doing is more specific since DST is already applied.
I found one function on PHP.net that seems to do the trick (it works for my timezone and returns the correct time) although for other timezones such as PST, it's returning 1 hour later than it should be even though the offset is correct (-07:00 with DST).
The timezone returned from the function is Chile/EasterIsland which I have a feeling is the cause. If I could, I would make this only work for the USA, but I do need it to be worldwide.
This is the function I have now. Please excuse the extremely messy code. I have been playing around with tons of things over the last few hours trying to figure out a solution.
Most of the functionality was found online.
function offsetToTZ($offset) {
switch((string) $offset) {
case '-04:30' : return 'America/Caracas'; break;
case '-03:30' : return 'Canada/Newfoundland'; break;
case '+03:30' : return 'Asia/Tehran'; break;
case '+04:30' : return 'Asia/Kabul'; break;
case '+05:30' : return 'Asia/Kolkata'; break;
case '+05:45' : return 'Asia/Kathmandu'; break;
case '+09:30' : return 'Australia/Darwin'; break;
}
$offset = (int) str_replace(array('0',0,':00',00,'30',30,'45',45,':','+'),'', (string) $offset);
$offset = $offset*60*60;
$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr) {
foreach($abbr as $city) {
if($city['offset'] == $offset) {
return $city['timezone_id'];
}
}
}
return false;
}
I included the switch/case for certain timezones that are :30 and :45 out there. There may be a way to include that also without the need of the switch/case.
NOTE: The offsets are always returned as such +00:00 or -00:00 from the geo IP API.
I would appreciate any help or a point in the right direction. I'm not very novice with PHP, but offsets are a new story for me. Thanks!
It can be done quite simply, by turning the offset into seconds and passing it to timezone_name_from_abbr:
<?php
$offset = '-7:00';
// Calculate seconds from offset
list($hours, $minutes) = explode(':', $offset);
$seconds = $hours * 60 * 60 + $minutes * 60;
// Get timezone name from seconds
$tz = timezone_name_from_abbr('', $seconds, 1);
// Workaround for bug #44780
if($tz === false) $tz = timezone_name_from_abbr('', $seconds, 0);
// Set timezone
date_default_timezone_set($tz);
echo $tz . ': ' . date('r');
Demo
The third parameter of timezone_name_from_abbr controls whether to adjust for daylight saving time or not.
Bug #44780:
timezone_name_from_abbr() will return false on some time zone
offsets. In particular - Hawaii, which has a -10 from GMT offset, -36000
seconds.
References:
timezone_name_from_abbr
date_default_timezone_set
date
date_default_timezone_set('UTC');
$timezones = array();
foreach (DateTimeZone::listAbbreviations() as $key => $array)
{
$timezones = array_merge($timezones, $array);
}
$utc = new DateTimeZone('UTC');
$timezone_offset = '+02:00'; # 2H
$sign = substr($timezone_offset, 0, 1) == '+'? '': '-';
$offset = substr($timezone_offset, 1, 2) . 'H' . substr($timezone_offset, 4, 2) . 'M';
$operation = $sign == ''? 'add': 'sub';
$start = new DateTime('', $utc);
$date = new DateTime('', $utc);
$date->{$operation}(new DateInterval("PT{$offset}"));
$offset = $start->diff($date)->format('%r') . ($start->diff($date)->h * 3600 + $start->diff($date)->m * 60 + $start->diff($date)->s); # 7200 (2H)
echo $offset, PHP_EOL;
echo $date->format('Y-m-d H:i:s'), PHP_EOL;
foreach($timezones as $timezone)
{
if($timezone['offset'] == $offset)
{
echo $timezone['timezone_id'], PHP_EOL;
}
}
I might have misunderstood you in some parts, but I hope it helps, if you could be more specific, I might be more helpful.
For Chile I get:
-25200 (-7h)
2012-08-07 18:05:24 (current time 2012-08-08 01:05:24)
Chile/EasterIsland
Output of the example above:
7200
2012-08-08 02:49:56
Europe/London
Europe/Belfast
Europe/Gibraltar
Europe/Guernsey
Europe/Isle_of_Man
Europe/Jersey
GB
Africa/Khartoum
Africa/Blantyre
Africa/Bujumbura
Africa/Gaborone
Africa/Harare
Africa/Kigali
Africa/Lubumbashi
Africa/Lusaka
Africa/Maputo
Africa/Windhoek
Europe/Berlin
Africa/Algiers
Africa/Ceuta
Africa/Tripoli
Africa/Tunis
Arctic/Longyearbyen
Atlantic/Jan_Mayen
CET
Europe/Amsterdam
Europe/Andorra
Europe/Athens
Europe/Belgrade
Europe/Bratislava
Europe/Brussels
Europe/Budapest
Europe/Chisinau
Europe/Copenhagen
Europe/Gibraltar
Europe/Kaliningrad
Europe/Kiev
Europe/Lisbon
Europe/Ljubljana
Europe/Luxembourg
Europe/Madrid
Europe/Malta
Europe/Minsk
Europe/Monaco
Europe/Oslo
Europe/Paris
Europe/Podgorica
Europe/Prague
Europe/Riga
Europe/Rome
Europe/San_Marino
Europe/Sarajevo
Europe/Simferopol
Europe/Skopje
Europe/Sofia
Europe/Stockholm
Europe/Tallinn
Europe/Tirane
Europe/Tiraspol
Europe/Uzhgorod
Europe/Vaduz
Europe/Vatican
Europe/Vienna
Europe/Vilnius
Europe/Warsaw
Europe/Zagreb
Europe/Zaporozhye
Europe/Zurich
WET
Europe/Kaliningrad
Europe/Helsinki
Africa/Cairo
Africa/Tripoli
Asia/Amman
Asia/Beirut
Asia/Damascus
Asia/Gaza
Asia/Istanbul
Asia/Nicosia
EET
Europe/Athens
Europe/Bucharest
Europe/Chisinau
Europe/Istanbul
Europe/Kaliningrad
Europe/Kiev
Europe/Mariehamn
Europe/Minsk
Europe/Moscow
Europe/Nicosia
Europe/Riga
Europe/Simferopol
Europe/Sofia
Europe/Tallinn
Europe/Tiraspol
Europe/Uzhgorod
Europe/Vilnius
Europe/Warsaw
Europe/Zaporozhye
Asia/Jerusalem
Asia/Gaza
Asia/Tel_Aviv
MET
Africa/Johannesburg
Africa/Maseru
Africa/Mbabane
Africa/Windhoek
Africa/Windhoek
Africa/Ndjamena
Europe/Lisbon
Europe/Madrid
Europe/Monaco
Europe/Paris
WET
Europe/Luxembourg
Which nails my timezone.
$UTC_offset = '+03:00';
$date = new \DateTime('now', 'UTC');
var_dump($date);
$timezone = new \DateTimeZone(str_replace(':', '', $UTC_offset));
$date->setTimezone($timezone);
var_dump($date);
Results:
class DateTime#205 (3) {
public $date =>
string(26) "2015-01-20 06:00:00.000000"
public $timezone_type =>
int(3)
public $timezone =>
string(3) "UTC"
}
class DateTime#205 (3) {
public $date =>
string(26) "2015-01-20 09:00:00.000000"
public $timezone_type =>
int(1)
public $timezone =>
string(6) "+03:00"
}
It is unadvised to map time zone offsets back to a time zone identifier. Many time zones share the same offset.
Even within a single country, this is problematic. Consider that in the United States, the -5 offset is used by both Central Standard Time (America/Chicago) and Eastern Daylight Time (America/New_York) at different times of the year.
Also consider that there are times where BOTH of those time zones use -5 at the same time. For example, Sunday November 3rd 2013 at 1:00 AM in UTC-5 could be either in CDT or EST. You'd have no way to distinguish just by -5 which time zone it was.
Details here.
Nowadays the DateTimeZone constructor can explicitly accept a UTC offset, which I understand you have.
So just:
$timeZone = new DateTimeZone('+0100');
Documentation:
http://php.net/manual/en/datetimezone.construct.php
Note: per that documentation link, this new constructor usage has been available since PHP version 5.5.10.
You might want to have a look at DateTime PHP extension (it's enabled & included by default in all PHP versions >= 5.2.0, unless it was specifically disabled at compile time).
It does everything you need here quite well.
You can use GMT time also and convert it to your requirement afterwards
<?php
echo gmdate("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));
?>
GMT refers Greenwich Mean Time which is common all over the world.
This work for me:
function Get_Offset_Date($Offset, $Unixtime = false, $Date_Format = 'Y.m.d,H:i')
{
try
{
$date = new DateTime("now",new DateTimeZone($Offset));
if($Unixtime)
{
$date->setTimestamp($Unixtime);
}
$Unixtime = $date->getTimestamp();
return $date->format($Date_Format);
}
catch(Exception $e)
{
return false;
}
}
To use it just call this to get current time:
echo Get_Offset_Date('+2:00');
And this for get Offset date (input is unixtime)
echo Get_Offset_Date('+2:00',1524783867);
Related
I need to detect when a timestamp is in daylight saving or not.
I'm using this code to test the functionality:
<?php
date_default_timezone_set('Europe/Berlin');
$timestamp = $baseTimestamp = 1509234900; // Sunday, 29 October of 2017 1:55:00 GMT+02:00 DST
$date = (new DateTime)->setTimestamp($baseTimestamp);
echo "DateTime\t\t| Is in summer \t| Minutes passed\n";
for($i = 0; $i < 70; $i++) {
$date = (new DateTime)->setTimestamp($timestamp);
echo $date->format("Y-m-d H:i:s \t|I") . "\t\t| " . ($timestamp - $baseTimestamp)/60 . "\n";
$timestamp = $timestamp + 60;
}
https://3v4l.org/dqNlK
Working with Europe/Berlin, I've seen that in March, when at 2.00 we pass from winter to summer, php solves it right, but then in October, when it is supposed to come back from summer to winter at 3.00, it doesn't work as expected.
In this case, we have two timestamps corresponding to the same hour (at 3 is 2 again), but the timestamp is unique, so for 2.00 there must be one timestamp in the summer time and another one in the winter time.
Using hhvm, it shows the right value, but normal php interpreters show that is not in summer for both 2.00 (the first one, which is 2am and the second one which is when at 3am is 2am again. This is the one that should say is not in summer anymore)
Yes, there are several long-lived, unresolved, DST-related bugs in PHP.
You appear to have hit on this one:
https://bugs.php.net/bug.php?id=68549
function isSummer($timestamp) {
$date = (new DateTime)->setTimestamp($timestamp);
if ($date->format('I') == 1) {
return true;
}
return ($date->getTimestamp() > $timestamp);
}
I think this is going to do the trick
I need a function which returns the number of days since 1.1.1970 but depending on the timezone.
I wrote the following function but was wondering if there is a better way to do it (some better way to determinate the offset from gmt would be a good thing).
function getDaysSinceUnix($time = null){
if($time === null){$time = time();}
$day = 24*60*60;
$offset = intval(substr(date('O'),0,-2))*60*60;
return intval(floor(($time+$offset)/$day));
}
If not, is there something you would add that could give this function more stability ?
Using DateTime:
function getDaysSinceUnix( $time = 0, $timeZone = Null )
{
$time = "#$time";
$tz = new DateTimeZone( $timeZone ?: date_default_timezone_get() );
return date_create( $time )->setTimeZone( $tz )->diff( date_create( '1970-01-01', $tz ) )->days;
}
If no Time Zone is passed, current Time Zone is used.
The Carbon library is pretty much the gold standard for PHP date/time work.
Carbon\Carbon::createFromTimestamp(0)->diffInDays();
I am saving all the times in MySQL in UTC(0), so I could change them later while I'm showing the times for the users with different time zones, for saving into db, I use:
function get_utc(){
date_default_timezone_set('UTC');
return date("Y-m-d H:i:s");
}
$now = get_utc();
And now I want to convert those times into different timezones based by timezones offset, I am using this function:
function utc_and_timezone($utc_time, $offset) {
return date("Y-m-d H:i:s", strtotime($offset, strtotime($utc_time)));
}
So for example, if the UTC time is: 2013-01-30 21:06:29
Applying the +5 timezone on that time is easy to find:
$new_time = utc_and_timezone("2013-01-30 21:06:29", "+5 hours"); // Works Fine
It works JUST fine with offsets like 5,6 or other integers, BUT with some other like +3.5, +2.5 this is not working:
$new_time = utc_and_timezone("2013-01-30 21:06:29", "+5.5 hours"); // NOT WORKING
Anyone knows why?!
Any better solutions for making UTC times in different timezones...?
Thanks
SHORT QUESTION:
I want to show a UTC time like 2013-01-30 21:06:29 in +3.5 timezone, how is that possible?
On the PHP side, you can try:
$utc_time = '2013-01-30 21:06:29';
$offset = '3.5';
echo date("Y-m-d H:i:s", strtotime($utc_time) + (3600 * $offset) );
//Returns 2013-01-31 00:36:29
$utc_time = '2013-01-30 21:06:29';
$offset = '-5.5';
echo date("Y-m-d H:i:s", strtotime($utc_time) + (3600 * $offset) );
//Returns 2013-01-30 15:36:29
On the MySQL side, you can just use CONVERT_TZ:
SELECT CONVERT_TZ('2013-01-30 21:06:29','+00:00','+03:30');
//Returns January, 31 2013 00:56:29+0000
SELECT CONVERT_TZ('2013-01-30 21:06:29','+00:00','-05:50');
//Returns January, 30 2013 15:16:29+0000
A head scratcher for you.
I am grabbing geo IP data from IPInfoDB's API and it returns a timezone offset from UTC including DST (if currently reflected).
For example, I live in EST (-5) and currently it's DST, so the geo IP API returns (-04:00) as the offset.
This is wonderful since DST is a freaking headache. But to my surprise, it caused another headache.
I load this data in PHP to be passed via AJAX to the application. I would like to have the live local time of the IP address on the app.
I have that all set perfectly, but I am going crazy trying to figure out how to set the PHP timezone to match the offset so I can just grab the current hours date('H'); and minutes date('i'); to pass to via AJAX.
I am unsure if there is a specific function that can just give me the current hours and minutes based on that offset or if there is a practical way to set the timezone based on the offset (which will have DST already applied if is in effect).
I've been searching and searching Google to find an answer to this, but what I am doing is more specific since DST is already applied.
I found one function on PHP.net that seems to do the trick (it works for my timezone and returns the correct time) although for other timezones such as PST, it's returning 1 hour later than it should be even though the offset is correct (-07:00 with DST).
The timezone returned from the function is Chile/EasterIsland which I have a feeling is the cause. If I could, I would make this only work for the USA, but I do need it to be worldwide.
This is the function I have now. Please excuse the extremely messy code. I have been playing around with tons of things over the last few hours trying to figure out a solution.
Most of the functionality was found online.
function offsetToTZ($offset) {
switch((string) $offset) {
case '-04:30' : return 'America/Caracas'; break;
case '-03:30' : return 'Canada/Newfoundland'; break;
case '+03:30' : return 'Asia/Tehran'; break;
case '+04:30' : return 'Asia/Kabul'; break;
case '+05:30' : return 'Asia/Kolkata'; break;
case '+05:45' : return 'Asia/Kathmandu'; break;
case '+09:30' : return 'Australia/Darwin'; break;
}
$offset = (int) str_replace(array('0',0,':00',00,'30',30,'45',45,':','+'),'', (string) $offset);
$offset = $offset*60*60;
$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr) {
foreach($abbr as $city) {
if($city['offset'] == $offset) {
return $city['timezone_id'];
}
}
}
return false;
}
I included the switch/case for certain timezones that are :30 and :45 out there. There may be a way to include that also without the need of the switch/case.
NOTE: The offsets are always returned as such +00:00 or -00:00 from the geo IP API.
I would appreciate any help or a point in the right direction. I'm not very novice with PHP, but offsets are a new story for me. Thanks!
It can be done quite simply, by turning the offset into seconds and passing it to timezone_name_from_abbr:
<?php
$offset = '-7:00';
// Calculate seconds from offset
list($hours, $minutes) = explode(':', $offset);
$seconds = $hours * 60 * 60 + $minutes * 60;
// Get timezone name from seconds
$tz = timezone_name_from_abbr('', $seconds, 1);
// Workaround for bug #44780
if($tz === false) $tz = timezone_name_from_abbr('', $seconds, 0);
// Set timezone
date_default_timezone_set($tz);
echo $tz . ': ' . date('r');
Demo
The third parameter of timezone_name_from_abbr controls whether to adjust for daylight saving time or not.
Bug #44780:
timezone_name_from_abbr() will return false on some time zone
offsets. In particular - Hawaii, which has a -10 from GMT offset, -36000
seconds.
References:
timezone_name_from_abbr
date_default_timezone_set
date
date_default_timezone_set('UTC');
$timezones = array();
foreach (DateTimeZone::listAbbreviations() as $key => $array)
{
$timezones = array_merge($timezones, $array);
}
$utc = new DateTimeZone('UTC');
$timezone_offset = '+02:00'; # 2H
$sign = substr($timezone_offset, 0, 1) == '+'? '': '-';
$offset = substr($timezone_offset, 1, 2) . 'H' . substr($timezone_offset, 4, 2) . 'M';
$operation = $sign == ''? 'add': 'sub';
$start = new DateTime('', $utc);
$date = new DateTime('', $utc);
$date->{$operation}(new DateInterval("PT{$offset}"));
$offset = $start->diff($date)->format('%r') . ($start->diff($date)->h * 3600 + $start->diff($date)->m * 60 + $start->diff($date)->s); # 7200 (2H)
echo $offset, PHP_EOL;
echo $date->format('Y-m-d H:i:s'), PHP_EOL;
foreach($timezones as $timezone)
{
if($timezone['offset'] == $offset)
{
echo $timezone['timezone_id'], PHP_EOL;
}
}
I might have misunderstood you in some parts, but I hope it helps, if you could be more specific, I might be more helpful.
For Chile I get:
-25200 (-7h)
2012-08-07 18:05:24 (current time 2012-08-08 01:05:24)
Chile/EasterIsland
Output of the example above:
7200
2012-08-08 02:49:56
Europe/London
Europe/Belfast
Europe/Gibraltar
Europe/Guernsey
Europe/Isle_of_Man
Europe/Jersey
GB
Africa/Khartoum
Africa/Blantyre
Africa/Bujumbura
Africa/Gaborone
Africa/Harare
Africa/Kigali
Africa/Lubumbashi
Africa/Lusaka
Africa/Maputo
Africa/Windhoek
Europe/Berlin
Africa/Algiers
Africa/Ceuta
Africa/Tripoli
Africa/Tunis
Arctic/Longyearbyen
Atlantic/Jan_Mayen
CET
Europe/Amsterdam
Europe/Andorra
Europe/Athens
Europe/Belgrade
Europe/Bratislava
Europe/Brussels
Europe/Budapest
Europe/Chisinau
Europe/Copenhagen
Europe/Gibraltar
Europe/Kaliningrad
Europe/Kiev
Europe/Lisbon
Europe/Ljubljana
Europe/Luxembourg
Europe/Madrid
Europe/Malta
Europe/Minsk
Europe/Monaco
Europe/Oslo
Europe/Paris
Europe/Podgorica
Europe/Prague
Europe/Riga
Europe/Rome
Europe/San_Marino
Europe/Sarajevo
Europe/Simferopol
Europe/Skopje
Europe/Sofia
Europe/Stockholm
Europe/Tallinn
Europe/Tirane
Europe/Tiraspol
Europe/Uzhgorod
Europe/Vaduz
Europe/Vatican
Europe/Vienna
Europe/Vilnius
Europe/Warsaw
Europe/Zagreb
Europe/Zaporozhye
Europe/Zurich
WET
Europe/Kaliningrad
Europe/Helsinki
Africa/Cairo
Africa/Tripoli
Asia/Amman
Asia/Beirut
Asia/Damascus
Asia/Gaza
Asia/Istanbul
Asia/Nicosia
EET
Europe/Athens
Europe/Bucharest
Europe/Chisinau
Europe/Istanbul
Europe/Kaliningrad
Europe/Kiev
Europe/Mariehamn
Europe/Minsk
Europe/Moscow
Europe/Nicosia
Europe/Riga
Europe/Simferopol
Europe/Sofia
Europe/Tallinn
Europe/Tiraspol
Europe/Uzhgorod
Europe/Vilnius
Europe/Warsaw
Europe/Zaporozhye
Asia/Jerusalem
Asia/Gaza
Asia/Tel_Aviv
MET
Africa/Johannesburg
Africa/Maseru
Africa/Mbabane
Africa/Windhoek
Africa/Windhoek
Africa/Ndjamena
Europe/Lisbon
Europe/Madrid
Europe/Monaco
Europe/Paris
WET
Europe/Luxembourg
Which nails my timezone.
$UTC_offset = '+03:00';
$date = new \DateTime('now', 'UTC');
var_dump($date);
$timezone = new \DateTimeZone(str_replace(':', '', $UTC_offset));
$date->setTimezone($timezone);
var_dump($date);
Results:
class DateTime#205 (3) {
public $date =>
string(26) "2015-01-20 06:00:00.000000"
public $timezone_type =>
int(3)
public $timezone =>
string(3) "UTC"
}
class DateTime#205 (3) {
public $date =>
string(26) "2015-01-20 09:00:00.000000"
public $timezone_type =>
int(1)
public $timezone =>
string(6) "+03:00"
}
It is unadvised to map time zone offsets back to a time zone identifier. Many time zones share the same offset.
Even within a single country, this is problematic. Consider that in the United States, the -5 offset is used by both Central Standard Time (America/Chicago) and Eastern Daylight Time (America/New_York) at different times of the year.
Also consider that there are times where BOTH of those time zones use -5 at the same time. For example, Sunday November 3rd 2013 at 1:00 AM in UTC-5 could be either in CDT or EST. You'd have no way to distinguish just by -5 which time zone it was.
Details here.
Nowadays the DateTimeZone constructor can explicitly accept a UTC offset, which I understand you have.
So just:
$timeZone = new DateTimeZone('+0100');
Documentation:
http://php.net/manual/en/datetimezone.construct.php
Note: per that documentation link, this new constructor usage has been available since PHP version 5.5.10.
You might want to have a look at DateTime PHP extension (it's enabled & included by default in all PHP versions >= 5.2.0, unless it was specifically disabled at compile time).
It does everything you need here quite well.
You can use GMT time also and convert it to your requirement afterwards
<?php
echo gmdate("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));
?>
GMT refers Greenwich Mean Time which is common all over the world.
This work for me:
function Get_Offset_Date($Offset, $Unixtime = false, $Date_Format = 'Y.m.d,H:i')
{
try
{
$date = new DateTime("now",new DateTimeZone($Offset));
if($Unixtime)
{
$date->setTimestamp($Unixtime);
}
$Unixtime = $date->getTimestamp();
return $date->format($Date_Format);
}
catch(Exception $e)
{
return false;
}
}
To use it just call this to get current time:
echo Get_Offset_Date('+2:00');
And this for get Offset date (input is unixtime)
echo Get_Offset_Date('+2:00',1524783867);
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.
hope this will help you
<?php
date_default_timezone_set('Europe/Sofia');
echo date_default_timezone_get(); // Europe/Sofia
echo ' => '.date('T'); // => EET
?>
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
Hope this will help you
<?php
$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone('America/Havana'));
echo $dateTime->format('T');
?>
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
Looks like Symphony has methods for that, e.g. select_timezone_tag. You might check their source code to see how it's done.