• I have a homegrown WordPress plugin that has always used “date_default_timezone_set” to set the script to work at the same timezone as the WP blog is set for.

    Current code:

    {
        if ( $timezone = get_option( 'timezone_string' ) )
            return $timezone;
    }
    	date_default_timezone_set(wp_get_timezone_string());

    Although the plugin and the websites still work fine, the WordPress dashboards now report the use of “date_default_timezone_set” as a critical issue.

    I need a new way to set the timezone in the script to match whatever zone the WP site is set for. But I can’t find reference to that anywhere, and experimentation hasn’t done it. I know others have solved this new issue, but I haven’t been able to find the solution.

    How to I set my php script to use whatever timezone the WP website is set for?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Looks like there were some changes to the timestamp API usage in Ver 5.3. This post may help you

    Date/Time component improvements in WordPress 5.3

    Thread Starter Mike Hickcox

    (@mike-hickcox)

    Thanks. I appreciate the reference to that info. What I cannot find there is the answer to this:

    Up to now, “date_default_timezone_set” could be used to designate the zone for the plugin to use. Now that we’re not supposed to use that, how do we designate time in the plugin?

    I admit being a beginner here ??

    I found some information though on this thread written by a moderator during the PHP that may provide some more clues
    https://www.ads-software.com/support/topic/read-this-first-wordpress-5-3-master-list/#post-12124062

    One such thing would be to call the date_default_timezone_set() function in PHP. WordPress works by setting the default timezone to UTC and then performing its own calculations to adjust times. Setting the default timezone incorrectly to anything else will result in these calculations being incorrect. WordPress will be overcompensating the timezone adjustments.

    For backwards compatibility, it is crucial that plugins *not* change the default PHP timezone. The default timezone must be set to UTC at all times. Any problems you find with timezones or dates shifting is likely going to be a plugin or theme which is doing-it-wrong. This may not have mattered as much in previous versions of WordPress, but now that WordPress is using these newer functions more and less reliant on the old PHP 4 compatible code, then this is far more critical.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    The normal WordPress setting is date_default_timezone_set('UTC');, and you can find this in the wp-settings.php file. The default timezone should not be set to the timezone string, that is calculated differently.

    So, WordPress expects the default timezone to always be UTC. This is largely for legacy reasons, but yes, if you change this, then it will mess with WordPress’s calculations for time handling.

    If you need to get times in a plugin, you should probably use the relevant WordPress functions to do it.

    If you need to do timezone calculation and such, then setting the default timezone is not usually actually necessary, as the relevant PHP functions can mostly take the timezone as an argument to them, in some fashion.

    If for some extreme reason, you absolutely need your plugin to call date_default_timezone_set and there is no possible way that you can find to not do that, then you should call date_default_timezone_get() first, save its return value, and then call date_default_timezone_set() with this value afterwards, so as to set the default timezone back to what WordPress expects it to be, after you have done whatever it is that you need to do.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to set blog time in Script w/o date_default_timezone_set’ is closed to new replies.