Skip to content

Instantly share code, notes, and snippets.

@EpokK
Last active January 3, 2016 13:29
Show Gist options
  • Save EpokK/8469685 to your computer and use it in GitHub Desktop.
Save EpokK/8469685 to your computer and use it in GitHub Desktop.
How to retrieve timezone offset from GMT+0 in php?
<?php
public function getStandardOffsetUTC($timezone)
{
if($timezone == 'UTC') {
return '';
} else {
$timezone = new DateTimeZone($timezone);
$transitions = array_slice($timezone->getTransitions(), -3, null, true);
foreach (array_reverse($transitions, true) as $transition)
{
if ($transition['isdst'] == 1)
{
continue;
}
return sprintf('UTC %+03d:%02u', $transition['offset'] / 3600, abs($transition['offset']) % 3600 / 60);
}
return false;
}
}
// usage
echo getStandardOffsetUTC('Europe/Sofia');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment