• I have a small caching script which I’m running between sunrise and sunset. Strangely when setting the current time using PHP, it’s different before from after loading WP:

    <?php
    echo date('Gi'); //correct time
    include_once $_SERVER['DOCUMENT_ROOT'].'/wp-load.php';
    echo date('Gi'); //time off by 2 hours, which is UTC
    ?>

    I have set the correct timezone in WP and the correct time is showing in the main settings screen.

    “Universal time (UTC) is 2017-04-09 07:18:12. Local time is 2017-04-09 09:18:12.”

    Also the PHP timezone is set correctly on the server.

    Strange or usual behaviour?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Oddly, no. Your first call is based on your server’s default localization settings, which normally has date() returning local time. When you load the WP environment, it alters the server’s localization settings to ensure the timezone corrections are consistently applied. If this is not done, the adjustment could be applied twice or not at all, depending on the server. The changes only apply to the remainder of the request, they do not persist. Specifically, WP wants date() to return the UTC time. It then applies it’s own timezone settings based on the admin settings screen selection.

    All WP related time functions will consistently apply the correct adjustments and WP functions like current_time() will return the correct values. In your own WP coding, you do need to be aware that PHP time functions are returning UTC times. If you want local time, use date_i18n() instead of date().

    Thread Starter vespino

    (@vespino)

    Thanks, I will be using date_i18n() in my script.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WordPress PHP date’ is closed to new replies.