[wp-hackers] Date/Time and WP

William P. Davis will.davis at gmail.com
Wed Jul 17 12:56:47 UTC 2013


date_i18n is a fantastic function that will make sure the date and time is returned in the same timezone set in the WP settings
Sent from my BlackBerry

-----Original Message-----
From: Ryan McCue <lists at rotorised.com>
Sender: "wp-hackers" <wp-hackers-bounces at lists.automattic.com>Date: Wed, 17 Jul 2013 22:48:02 
To: <wp-hackers at lists.automattic.com>
Reply-To: wp-hackers at lists.automattic.com
Subject: Re: [wp-hackers] Date/Time and WP

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;
		else
			$tzstring = 'Etc/GMT+' . $current_offset;
	}
	$zone = new DateTimeZone( $tzstring );
	return $zone;
}

-- 
Ryan McCue
<http://ryanmccue.info/>
_______________________________________________
wp-hackers mailing list
wp-hackers at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list