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!

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Leave a Reply

Your email address will not be published. Required fields are marked *


Copyright © 2024 Deluxe Blog Tips. All rights reserved.

My products: Meta Box | Slim SEO