wp_timezone_string()

Retrieves the timezone from site settings as a string.


Description Description

Uses the timezone_string option to get a proper timezone if available, otherwise falls back to an offset.


Top ↑

Return Return

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


Top ↑

Source 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

Changelog
Version Description
5.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Fida Al Hasan
    /**
     * If you want to show both timezone_string & gmt_offset
     * Preview: Asia/Dhaka [+06:00] or +06:00
     */
    
    // function goes to theme's functions.php file or plugin's file
    function wpdocs_custom_timezone_string() {
        $timezone_string = get_option( '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 );
        $timezone = $timezone_string ? $timezone_string . ' [' . $tz_offset . ']' : $tz_offset;
    
        return $timezone;
    }
    
    // Usage
    echo esc_html( wpdocs_custom_timezone_string() );
    

You must log in before being able to contribute a note or feedback.