• Guido

    (@guido07111975)


    Hi,

    I’m saving a date in my database with the strtotime() function and am looking for an approved way to force the timezone to be UTC. Currently I do something like this:

    my_function() {
    // get current timezone
    $current_zone = date_default_timezone_get();
    // set utc timezone for strtotime
    date_default_timezone_set( 'UTC' );

    // save my date
    update_post_meta( $post_id, 'date', sanitize_text_field( strtotime( $my_date_value ) ) );

    // set current timezone again
    date_default_timezone_set( $current_zone );
    }

    Works fine but not allowed according to the PCP:
    Using date_default_timezone_set() and similar isn’t allowed, instead use WP internal timezone support.

    Is there another way to do the same?

    (I did post about this before, but was not able to fix it. And it seems that PCP classified it as a warning at that time, but now it’s an error)

    Guido

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

    (@bcworkz)

    Hiya Guido!

    Maybe get_gmt_from_date() would work for you? It ought to be even simpler than what you have now. The date string parameter is assumed to be in the site’s local timezone. You needn’t temporarily alter the zone to get a UTC (GMT) value. You needn’t know what the local timezone is, the function knows.

    There are numerous WP date/time functions available to us. They are all based on the DateTimeImmutable PHP class, so presumably you could instead use any of the related functions if there is not a suitable WP function? IDK, I cannot speak for either of the review teams.

    I can see why we mustn’t use the old legacy date/time functions, doing so can make a mess of things. However, as long as we remain in the DateTimeImmutable paradigm, I don’t see why we couldn’t use related non-WP functions. It’s still best to use WP functions as long as they meet our needs.

    Thread Starter Guido

    (@guido07111975)

    Hi BC!

    I don’t use a timezone at all, so throughout the plugin it should be UTC(+0). My current approach works great, and I have no idea how to do the same with “WP supported” functions.

    so presumably you could instead use any of the related functions if there is not a suitable WP function

    That’s what I already do (check the code my first post).
    Unfortunately strtotime() doesn’t have a timezone parameter.

    Guido

    Thread Starter Guido

    (@guido07111975)

    How about adding the timezone UTC like this:

    strtotime("2025-10-081500 UTC+0")

    strtotime() does accept a time zone, but not certain if strtotime() will always respect and use this timezone? If it does, I have found what I was looking for.. 🙂

    Guido

    Moderator bcworkz

    (@bcworkz)

    Your plugin only uses UTC, but what are users inputting onto the text field? Are you asking them to do the offset calculation themselves? Wouldn’t it be more logical to take input based on the site’s timezone?

    Thinking about this some more, we cannot be sure the server’s timezone is the same as the WP timezone. Thus strtotime() alone does not work for applying the correct offset from UTC. The offset applied must be relative to the WP timezone. Native PHP functions cannot know what the WP timezone is. It must come from WP somehow. All WP time/date functions are aware of the site’s timezone setting.

    Additionally, strtotime() is one of the legacy functions. We should want to at least use functions derived from DateTimeImmutable if not actual WP functions.

    A comment added to the strtotime doc page, FWIW:

    [strtotime()] does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable.

    Thread Starter Guido

    (@guido07111975)

    Your plugin only uses UTC, but what are users inputting onto the text field? 

    Input uses date and time offset from Settings > General. But this is saved as UTC(+0) in database. Displayed on the frontend of the site without offset as well. So exactly the same as date and time that user has filled in. This is how the plugin is build.

    Thread Starter Guido

    (@guido07111975)

    Simply adding UTC does the trick and will ignore timezone from script.

    // set utc timezone for strtotime
    date_default_timezone_set( 'America/Toronto' );

    // display date
    echo strtotime( "2025-10-090800" ).'<br>';
    echo strtotime( "2025-10-090800 UTC" ).'<br>';

    Returns:
    1760011200 = 12:00:00 (so with offset)
    1759996800 = 08:00:00 (time user has filled in)

    You may argue whether this is the best approach, or that timezone should always be respected. But for me it has been working fine.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Force time zone in strtotime function’ is closed to new replies.