Skip to content

Instantly share code, notes, and snippets.

@DrewAPicture
Last active June 29, 2017 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrewAPicture/3c0ea6d25e7c13f51a030bf2eedb766f to your computer and use it in GitHub Desktop.
Save DrewAPicture/3c0ea6d25e7c13f51a030bf2eedb766f to your computer and use it in GitHub Desktop.
<?php
/**
* Attempts to retrieve the WP timezone, or if not set, the timezone derived from the gmt_offset.
*
* @access private
* @since 2.2
*
* @return string Timezone string, or if all checks fail, default is 'UTC'.
*/
private function get_core_timezone() {
// Passing a $default value doesn't work for the timezeon_string option.
$timezone = get_option( 'timezone_string' );
/*
* If the timezone isn't set, or rather was set to a UTC offset, core saves the value
* to the gmt_offset option and leaves timezone_string empty – because that makes
* total sense, obviously. ¯\_(ツ)_/¯
*
* So, try to use the gmt_offset to derive a timezone.
*/
if ( empty( $timezone ) ) {
// Try to grab the offset instead.
$gmt_offset = get_option( 'gmt_offset', 0 );
// Yes, core returns it as a string, so as not to confuse it with falsey.
if ( '0' !== $gmt_offset ) {
$timezone = timezone_name_from_abbr( '', (int) $gmt_offset * HOUR_IN_SECONDS, date( 'I' ) );
}
// If the offset was 0 or $timezone is still empty, just use 'UTC'.
if ( '0' === $gmt_offset || empty( $timezone ) ) {
$timezone = 'UTC';
}
}
return $timezone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment