ActionScheduler_TimezoneHelper::get_local_timezone_string
Helper to retrieve the timezone string for a site until a WP core method exists (see https://core.trac.wordpress.org/ticket/24730).
Adapted from wc_timezone_string() and https://secure.php.net/manual/en/function.timezone-name-from-abbr.php#89155.
If no timezone string is set, and its not possible to match the UTC offset set for the site to a timezone string, then an empty string will be returned, and the UTC offset should be used to set a DateTime's timezone.
Method of the class: ActionScheduler_TimezoneHelper{}
No Hooks.
Returns
String. PHP timezone string for the site or empty if no timezone string is available.
Usage
$result = ActionScheduler_TimezoneHelper::get_local_timezone_string( $reset );
- $reset(true|false)
- Unused.
Default:false
Changelog
| Since 2.1.0 | Introduced. |
ActionScheduler_TimezoneHelper::get_local_timezone_string() ActionScheduler TimezoneHelper::get local timezone string code WC 10.8.1
protected static function get_local_timezone_string( $reset = false ) {
// If site timezone string exists, return it.
$timezone = get_option( 'timezone_string' );
if ( $timezone ) {
return $timezone;
}
// Get UTC offset, if it isn't set then return UTC.
$utc_offset = intval( get_option( 'gmt_offset', 0 ) );
if ( 0 === $utc_offset ) {
return 'UTC';
}
// Adjust UTC offset from hours to seconds.
$utc_offset *= 3600;
// Attempt to guess the timezone string from the UTC offset.
$timezone = timezone_name_from_abbr( '', $utc_offset );
if ( $timezone ) {
return $timezone;
}
// Last try, guess timezone string manually.
foreach ( timezone_abbreviations_list() as $abbr ) {
foreach ( $abbr as $city ) {
if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) { // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date -- we are actually interested in the runtime timezone.
return $city['timezone_id'];
}
}
}
// No timezone string.
return '';
}