public
Created

Returns the blog timezone as a DateTimeZone object

  • Download Gist
wp-blog-timezone.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
<?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;
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.