How to retrieve timezone offset from GMT+0 in php?
This will be useful for converting offset values into GMT format without any calculation
<?php //target time zone $target_time_zone = new DateTimeZone('America/Los_Angeles'); //find kolkata time $date_time = new DateTime('now', $target_time_zone); //get the exact GMT format echo 'GMT '.$date_time->format('P');
This is a simple example how to get timezone offset in seconds:
$dtz = new DateTimeZone('Europe/Sofia');$time_in_sofia = new DateTime('now', $dtz);echo $dtz->getOffset( $time_in_sofia );
to display it in the format GMT+x:
$offset = $dtz->getOffset( $time_in_sofia ) / 3600;echo "GMT" . ($offset < 0 ? $offset : "+".$offset);
This will get you the standard offset (not the one in or out of DST [depending on the time of year]):
function getStandardOffsetUTC($timezone){ if($timezone == 'UTC') { return ''; } else { $timezone = new DateTimeZone($timezone); $transitions = $timezone->getTransitions(time() - 86400*365, time()); foreach ($transitions as $transition) { if ($transition['isdst']) { continue; } return sprintf('UTC %+03d:%02u', $transition['offset'] / 3600, abs($transition['offset']) % 3600 / 60); } return false; }}
Usage:
echo getStandardOffsetUTC('Europe/Sofia'); // UTC +02:00
You can get a peek at the differences here.