Categories: Tutorials

How to solve Current Date and timezone issue in WordPress

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 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') &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 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.

This post was last modified on 26/06/2018 10:34

Boris Hoekmeijer

View Comments

  • 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

  • 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?

  • 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'));

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

    Ken

  • 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 ;-) )!

Share
Published by
Boris Hoekmeijer

Recent Posts

Agribusiness Service

Website en logo ontwerp voor Kookstudio Aalsmeer - voor uw kookclub, kookfeest of bedrijfsuitje in…

4 months ago

CardioThoracaal Chirurgie Zorgpad

Website en logo ontwerp voor Kookstudio Aalsmeer - voor uw kookclub, kookfeest of bedrijfsuitje in…

4 months ago

7huijzen Food Quality & Innovation Management

Website en logo ontwerp voor Kookstudio Aalsmeer - voor uw kookclub, kookfeest of bedrijfsuitje in…

6 months ago

Prime Housing

Website en logo ontwerp voor Kookstudio Aalsmeer - voor uw kookclub, kookfeest of bedrijfsuitje in…

6 months ago

Cor de Kroon

Website en logo ontwerp voor Kookstudio Aalsmeer - voor uw kookclub, kookfeest of bedrijfsuitje in…

8 months ago

Hoe leeg je de cache van je browser?

Building your WordPress website efficiently using the 10 tips I documented after years of experience.…

1 year ago