Created
May 18, 2012 10:23
-
-
Save stephenh1988/2724520 to your computer and use it in GitHub Desktop.
Returns the blog timezone as a DateTimeZone object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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