Subscribe to
Posts
Comments
NSLog(); Header Image

WordPress 2.9 – When’s the Time Zone Set?

I have a few headers that rotate on this blog, but on a few days with known dates (Thanksgiving doesn't qualify, but Halloween, my birthday, and Christmas do) I put in a special banner that lasts all day.

Simple bit of code, really:

switch(date('md'))
{
    case '0101':
        return '_newyear'; break;
    case '0323':
        return '_birthday'; break;
    case '1031':
        return '_halloween'; break;
    case '1225':
        return '_christmas'; break;
    default:
        break;
};

At 8:15 this evening, though, my wife noticed that my Christmas banner was already up.

I investigated. The first thing I did was to check the system time. It was normal and matched the eastern time zone where I live.

Next I checked echo date('Y-m-d H:i:s'); on a test PHP file on the server. Also normal.

Finally I put some code into WordPress, and this is where the problems started to pop up.

This code:

echo "<!-- date('Y-m-d H:i:s') reports: " . date('Y-m-d H:i:s') . " => " . date_default_timezone_get() . date(' e ') . date(' T ') . " -->";

Reported:

date('Y-m-d H:i:s') reports as: 2009-12-25 01:16:14 => UTC UTC UTC

Huh? It's five hours ahead. And why is UTC being reported as the time zone? My WordPress settings are set to "New York" and my server has the proper date and time. Why is this showing up?

If I add this before those two lines of code:

date_default_timezone_set('America/New_York');

Then the results are:

date('Y-m-d H:i:s') reports as: 2009-12-24 20:19:48 => America/New_York America/New_York EST

So okay, the WordPress machinery doesn't quite kick in as early as I'd like for the header image. Adding date_default_timezone_set('America/New_York'); to the header image rotator right before I call date('md'); solves the problem.

But why, when PHP stand-alone reports the right time, does the same call not work at the top of header.php in WordPress?

That's a question (and possibly a bug, though an odd one) for another day. It's Christmas Eve and I've got some cleaning to do. 🙂

P.S. Quick testing shows that WordPress changes the time zone sometime around get_header(); as calls to PHP's date() one line before and one line after get_header() show different times.

5 Responses to "WordPress 2.9 – When’s the Time Zone Set?"

  1. I was having the same problem. If you look in the wp-settings.php file, you'll find that the default timezone is set to 'UTC'. This is what is causing the time discrepancy. Comment out the 'if' statement (lines 21 & 22) in this file, and everything should work properly, without having to set the timezone in your header.

  2. [quote comment="56577"]Comment out the 'if' statement (lines 21 & 22) in this file, and everything should work properly, without having to set the timezone in your header.[/quote]

    That did it. Awesome. I wonder if they consider this a bug… I'll have to go check and will consider filing one. It'll be a real pain to change that every time a new version of WordPress comes out. (TRAC #11665.)

  3. Todd and Erik, you both are lifesavers! I spent the entire week fuming over my website's broken PHP script and calling 1&1's Customer Service to complain that they screwed around with my domain's server.

    They kept telling me that it was my own code's fault, and when I backtracked through the past week's events, I realized the WordPress 2.9 update occurred around then too. I had my suspicions about WordPress.

  4. [...] time-zone settings within WordPress and it seemed correct. Today, I was referred to a blog post: WordPress 2.9 – When’s the Time Zone Set? which confirmed my suspicions AND resolved the [...]

  5. [...] the advice that I used from NSLog();'s post about WordPress still [...]


Comments RSS

Leave a Reply


Warning: Undefined variable $user_ID in /var/www/vhosts/nslog.com/httpsdocs/wp-content/themes/nslog/comments.php on line 96

Please abide by the comment policy. Valid HTML includes: <blockquote><p>, <em>, <strong>, <ul>, <ol>, and <a href>. Please use the "Quote Me" functionality to quote comments.