JayminKapish
Subscribe to jayminkapish RSS Friend Jaymin Patel on Facebook Follow Jaymin Patel on Twitter

« Go to home page

WordPress: Automatically adjust for daylight saving time

I have been googling for this and (again) I had to come up with my own solution.

function my_set_gmt_offset($offset) {
        global $wpdb;

        /* WordPress default sets to UTC timezone
        in wp-settings.php.
        you can change this line to whatever your timezone is.
        I set it to my timezone (NY)
        More: http://www.php.net/manual/en/timezones.php
        */

        if(function_exists('date_default_timezone_set'))
	        date_default_timezone_set('America/New_York');

	$timezone = date("T");

        /* Now we have your local timezone stored in $timezone
        so lets restore wordpress setting as in wp-settings.php */

        if ( function_exists('date_default_timezone_set') )
	       date_default_timezone_set('UTC');

	$current_offset = $offset;

	if($timezone == 'EDT') {
		$offset = '-4';
	}
	else if($timezone == 'EST') {
		$offset = '-5';
	}

	if($offset != $current_offset) {
		/* update offset in database if new offset is different.
		   Do not use update_option since it will call
                   this filter again and cause infinite loop.
		 */
		$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->options
		SET option_value = %s WHERE
		option_name = %s", $offset, 'gmt_offset' ) );

		/* let wordpress regenerate cache from database */
		wp_cache_set('gmt_offset', 'checkthedatabaseplease', 'options');
	}

	return $offset;
}

add_filter('option_gmt_offset', 'my_set_gmt_offset');

Share your feedback via comments.


One Response


Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


About

Wordpress Work

Plugins I like

Recent Posts

Wordpress News

Blogroll

Connect

I am excited about