Skip to content

Instantly share code, notes, and snippets.

@stephenh1988
Created May 18, 2012 10:23
Show Gist options
  • Save stephenh1988/2724520 to your computer and use it in GitHub Desktop.
Save stephenh1988/2724520 to your computer and use it in GitHub Desktop.
Returns the blog timezone as a DateTimeZone object
<?php
/*
* Returns the blog's timezone as a DateTimeZone object
*
*/
function sh_get_blog_timezone(){
$timezone = wp_cache_get( 'sh_blog_timezone' );
if ( false === $timezone || ! ($timezone instanceof DateTimeZone) ) {
$tzstring =get_option('timezone_string');
$offset = get_option('gmt_offset');
// Remove old Etc mappings. Fallback to gmt_offset.
if ( !empty($tzstring) && false !== strpos($tzstring,'Etc/GMT') )
$tzstring = '';
if( empty($tzstring) && $offset!=0 ):
//use offset
$offset *= 3600; // convert hour offset to seconds
$allowed_zones = timezone_abbreviations_list();
foreach ($allowed_zones as $abbr):
foreach ($abbr as $city):
if ($city['offset'] == $offset){
$tzstring=$city['timezone_id'];
break 2;
}
endforeach;
endforeach;
endif;
//Issue with the timezone selected, set to 'UTC'
if( empty($tzstring) ):
$tzstring = 'UTC';
endif;
$timezone = new DateTimeZone($tzstring);
wp_cache_set( 'sh_blog_timezone', $timezone );
}
return $timezone;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment