How to solve Current Date and timezone issue in WordPress
Relevant code in sidebar.php:
blank
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar()): else : ?> <!-- ..............Date&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>
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 WordPress support forum. 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 PHP manual website.
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') && dynamic_sidebar()): else : ?> <?php // set the default timezone to use. Available since PHP 5.1 date_default_timezone_set('Europe/Amsterdam'); ?> <!-- ..............Date&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 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 here.
– 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.
Thanks for the info,
I would like to add the date to archive pages, and other locations, I can in archive.php for example, but it’s always two hours too early. How should I use this code in archive.php and other pages?
Thanks a lot!
Arno
Got it working thanks a lot it. It shows up on some default places wqhere I want. How could I use it as a shortcode for widgets or in posts?
Thansk a lot, Arno
Hi Arno,
glad you got it working!
I can’t tell you how to use it as a shortcode. But I imagine googling on ‘create shortcode for wordpress’ would get you the information you need. The code itself is easy enough, and you have that already.
Please post your results here, it might help others!
Yes, you are absolutely correct…And it is very informative, very clear and Easy.Thanks
Thanks Boris for this time zone tip 🙂
Thank you for the terrific article
Thank you for this informative read, I have shared it on Twitter.
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?
I’ve never tried it myself, but this solution on Stackoverflow. Hope it helps!
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:
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’));
Hi Ron,
thanks for the useful addition!
Thank you for this. Like you, I found many solutions to this dilemma, but none of them really worked. Thanks for posting this.
Ken
Thanks for this nice post you’ve made!
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 😉 )!
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