Working with Date and Time in WordPress

Working with date and time is a very common task in general. In WordPress, you meet them when working with cron, fetching latest tweet, scheduling posts or delaying the RSS. Although PHP provides lots of date/time functions, WordPress has its own ones. In this post, I’m going to share some experience that I’ve had while working with date and time in WordPress.

Schedule

Timezone

The first thing you have to remember when working with date/time is timezone. If you miss this, your scheduled posts may be published in wrong time (like if I want to publish this post tomorrow morning at 7 AM and I set it for US time only, so I’ll see it at 6 PM!), your cron may not run as you expected, your email notifications will be sent late, etc. That’s totally a pain and very hard to debug because you have to wait to that time just to see how it happens!

WordPress has its settings for timezone in Settings -> General. And to retrieve this value, use this snippet:

$timezone_offet = get_option( 'gmt_offset' );

So if your time variable is GMT, to convert it into local time in WordPress, use this:

$timezone_offet = get_option( 'gmt_offset' );
$time += $timezone_offet * 3600; // $time is timestamp, we add the correct time offset here

Don’t use time()

time() is used to get current time in Unix timestamp format. WordPress has a wrapper function that handles both GMT time and local time (with timezone settings): current_time().

current_time( $type, $gmt = 0 );

This function returns the blog’s current local time in one of two formats, either MySQL’s timestamp data type format (i.e. YYYY-MM-DD HH:MM:SS) or the Unix timestamp format (i.e. epoch). The optional secondary parameter can be used to retrieve GMT time instead of the blog’s local time.

So, if your code use time(), change it to current_time( 'timestamp', 1 ), that will returns the GMT time. To get the local time, use current_time( 'timestamp' ). That’s all, easy to change and that makes more sense.

Date and time formats

WordPress let us change the default date and time format in Settings -> General. We should use these formats in our code instead of making a new format for a function like date(), unless you have your own reasons.

To retrieve these format, use this snippet:

$date_format = get_option( 'date_format' );
$time_format = get_option( 'time_format' );

And here’s an example that show current time in proper format:

$date_format = get_option( 'date_format' );
$time_format = get_option( 'time_format' );
echo date( "{$date_format} {$time_format}", current_time( 'timestamp' ) );

There’re a lot of things we can do with date and time in PHP, just note that we should use them in the WordPress way!

9 thoughts on “Working with Date and Time in WordPress

    1. I agree Julio. date_i18n() function is very important to localize the date for different languages.
      If we use just date() function, we see just English date strings.

      Regards.

  1. This has helped me so much I was looking all day on how to do this.
    I don’t really know what i changed but it finally changed it. I just have to check later on if it will change to next day at 12am.

    Thanks.

  2. Hello, after reading this post I think everybody knows about it but you have proven that you tackled it in a noble fashion. This is the kind of stuff everybody wants to read. It has certainly cleared lots of things up for me. Thanks a lot.

Comments are closed.

Newsletter

Subscribe to our newsletter to receive news and tutorials about WordPress and web development.

Memory - An elegant, clean and powerful WordPress blog theme! Learn more →