wp_timezone_string() WordPress Function

The wp_timezone_string() function is used to retrieve the name of the current timezone. This function is useful for plugins and themes that need to display dates and times in the correct timezone.

wp_timezone_string() #

Retrieves the timezone of the site as a string.


Description

Uses the timezone_string option to get a proper timezone name if available, otherwise falls back to a manual UTC ± offset.

Example return values:

  • ‘Europe/Rome’
  • ‘America/North_Dakota/New_Salem’
  • ‘UTC’
  • ‘-06:30’
  • ‘+00:00’
  • ‘+08:45’

Top ↑

Return

(string) PHP timezone name or a ±HH:MM offset.


Top ↑

Source

File: wp-includes/functions.php

function wp_timezone_string() {
	$timezone_string = get_option( 'timezone_string' );

	if ( $timezone_string ) {
		return $timezone_string;
	}

	$offset  = (float) get_option( 'gmt_offset' );
	$hours   = (int) $offset;
	$minutes = ( $offset - $hours );

	$sign      = ( $offset < 0 ) ? '-' : '+';
	$abs_hour  = abs( $hours );
	$abs_mins  = abs( $minutes * 60 );
	$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );

	return $tz_offset;
}


Top ↑

Changelog

Changelog
VersionDescription
5.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More