How to solve Current Date and timezone issue in WordPress

article header image

I ran into this issue while creating a custom theme for WordPress: I needed to display current date and time in the sidebar, but the time was off by two hours.

Relevant code in sidebar.php:

blank
<?php if (function_exists('dynamic_sidebar') &amp;&amp; dynamic_sidebar()): else : ?>
<!-- ..............Date&amp;Time block.............. -->
<li>
<div class="sidebarheader_calendar">
	<?php _e('Date'); ?>
	<br/>
	<p> <?php echo date('l, F j, Y'); ?> <?php echo date('G:i'); ?> </p>
</div>
</li>
This displayed both date and time allright, but the time was off by 2 hours.

An Internet search quickly showed me that I wasn’t the only one having this problem. The root cause of the issue seemed to be quite generic: In most of the fellow victims I encountered, the server appeared to be in a different time zone than the website owner/administrator. For some reason, the PHP code takes the timezone from the server instead of the timezone configured in the WordPress administrator.

Several forums and blogs posted (parts of) solutions to this issue. A few examples that seems to have worked for others:

Solution 1: Adding the following to your .htaccess file: SetEnv TZ Europe/London (fill in your own timezone, i.e. Europe/Amsterdam or EST or GMT).

Solution 2: Adding the following to your index.php:

$gmtOffset=get_option('gmt_offset');

Both these solutions were posted on the . Apparently they worked for others. But they didn’t work for me!

I tuned my search more toward PHP than WordPress, and found the solution at the .
By adding a simple piece of code to the sidebar.php just before the date & time PHP code, I solved the issue. Here’s the fully working code:

<?php if (function_exists('dynamic_sidebar') &amp;&amp; dynamic_sidebar()): else : ?>
<?php // set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('Europe/Amsterdam'); ?>    
<!-- ..............Date&amp;Time block.............. -->
<li>
<div class="sidebarheader_calendar">
	<?php _e('Date'); ?>
	<br/>
	<p><?php echo date('l, F j, Y'); ?> <?php echo date('G:i'); ?> </p>
</div>
</li>
The code explained:
– The first PHP line (everything inbetween

<?php and ?>

is for calling the dynamic (widget-enabled) sidebar.

– The second PHP line sets the timezone. For a full list of PHP timezones, check .

– The PHP code in the sidebarheader_calendar class actually shows date and time.

It’s a simple solution, but since I couldn’t find any articles that actually described this solution, I’m posting it here. Perhaps there are other solutions as well. If you know of any, post them here and I will adjust the article.


foto Boris Hoekmeijer
My name is Boris Hoekmeijer, I'm a webdesigner and graphic designer.
I sometimes run into a problem for which I have to find my own solution because Google just won't tell me. That's when I climb behind my mechanical keyboard, and fire away until a tutorial materializes. One of the great things about the web: we can all help each other!
If this article has helped you, or if you have anything to add or ask, please leave a comment or share the post.
Cheers!

© 2010 ★ Published: August 27, 2010
11 Comments

  • Lee Mac says:

    Thanks Boris for this time zone tip 🙂

  • Jasmine says:

    Thank you for the terrific article

  • Web Development says:

    Thank you for this informative read, I have shared it on Twitter.

  • Chris says:

    I just experienced this from a theme I bought. I posted an article but the Date posted is different from my current timezone. So changed my timezone on settings, but then after changing to my timezone, unfortunately when posting a new post it gets scheduled to be posted.

    What if my site is for registered users around the world who can post anything, then their post date will get affected too. Is there a way that WordPress fetches the timezone of that user and when the user posts, WP display users supposed timezone instead?

  • Ron says:

    It turns out what you really want to do, so that you don’t mess up the rest of WordPress’s time handling, is this:

    < ?php if (function_exists('dynamic_sidebar') && dynamic_sidebar()): else : ?>

  • < ?php _e('Date'); ?>

    < ?php echo date('l, F j, Y', current_time('timestamp')); ?> < ?php echo date('G:i', current_time('timestamp')); ?>


  • In case the markup in the code above gets filtered out, what you want to do is change your date calls from like this: echo date(‘I, F, j, Y’);
    to like this: echo date(‘I, F, j, Y’, current_time(‘timestamp’));

  • Ken says:

    Thank you for this. Like you, I found many solutions to this dilemma, but none of them really worked. Thanks for posting this.

    Ken

  • Drew Mc.Allister says:

    Thanks for this nice post you’ve made!

  • ispacecrusader says:

    No, I didn’t bother digging into that. I assume that the dutch version of WP only means that the information displayed to users is translated, the core will still be in english, which is where the date is retrieved. If you do find out, can you post it here? I’m sure there are others who’d also like to know (including myself 😉 )!

  • Rams says:

    Thanks for sharing, the time is FINALLY correct now.

    I now need to make it change from english date to the Dutch, any idea?
    Ive got the Dutch wordpress installed so it must be getting the ‘Friday’ from someplace else? Cheers

  • Leave a Reply

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

    NAVI

    Deze website maakt gebruik van cookies. Hoe zit dat?

    The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

    Close