[wp-hackers] Date/Time and WP

Ryan McCue lists at rotorised.com
Wed Jul 17 12:48:02 UTC 2013

Dobri wrote:

> From my understanding, when dealing with date and time, WordPress, because of PHP 4 legacy support, sets timezone to UTC and does its own calculations based on the timezone in options. So, when writing plugins, what would be the correct approach to making sure correct timezone is used? Should I use built-in functions like date and somehow specify timezone every time I access them? Or are there any WordPress functions created for that purpose?

See http://core.trac.wordpress.org/ticket/24730

Here's how I handle it:

protected function get_timezone() {
	$tzstring = get_option( 'timezone_string' );
	if ( ! $tzstring ) {
		// Create a UTC+- zone if no timezone string exists
		$current_offset = get_option( 'gmt_offset' );
		if ( 0 == $current_offset )
			$tzstring = 'UTC';
		elseif ($current_offset < 0)
			$tzstring = 'Etc/GMT' . $current_offset;
			$tzstring = 'Etc/GMT+' . $current_offset;
	$zone = new DateTimeZone( $tzstring );
	return $zone;

Ryan McCue

More information about the wp-hackers mailing list