[wp-hackers] Date/Time and WP
Dobri
dyordan1 at ramapo.edu
Wed Jul 17 12:57:49 UTC 2013
Thanks Ryan! When would a timezone_string not exist though? Wouldn't that simply mean settings were not touched, therefore it's some default (I'd guess UTC?)
~Dobri
On Wed, 17 Jul 2013, at 8:48 AM, Ryan McCue wrote:
> 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