Working with Date/Time Object in WordPress

There are many overlapping Date methods in PHP and WordPress. This article will show you the best way to handle Date object.

A year ago I was developing a Scheduling plugin for a client. I soon realized that there are many overlapping Date methods in PHP. Combined with the built-in WordPress methods, I was overwhelmed.

In this article, we will show you the best ways for working with Date object in WordPress.

1. DateTime Format

A quick reference on the common DateTime format. The complete list can be found in the official PHP doc.

YearY
y
2015
15
MonthM
m
F
Jan – Dec
01 – 12
January – December
DayD
d
Mon – Sun
01 – 31
HourH
h
A
00 – 23
01 – 12
AM or PM
Minutei01 – 59
Seconds01 – 59
TimestampUAmount of seconds from Jan 1970

2. Getting DateTime Object

CURRENT DATE:

$current_date = current_datetime();
echo $current_date->format( 'd/m/Y' );

// if you want to use the format from Settings:
echo $current_date->format(get_option('date_format'));

FROM POST ID:

$post_date = get_post_datetime($post_id);
echo $post_date->format('d/m/Y');

CONVERT FROM STRING:

$meta_date = '13/01/2020';
$datetime = DateTime::createFromFormat('d/m/Y', $meta_date);

echo $datetime->format('d M Y'); // 13 Jan 2020

CONVERT FROM SPECIAL STRING:

Published date of Posts and Comments are stored in the format Y-m-d H:i:s. If the string is in that format, you can use a shortcut to convert:

$post_date = '2020-01-16 12:00:00';
$datetime = new DateTime($post_date);

echo $datetime->format('d M Y'); // 16 Jan 2020

3. Distance Between DateTimes

$datetime1 = DateTime::createFromFormat('d/m/Y H:i', '10/02/2019 10:00');
$datetime2 = DateTime::createFromFormat('d/m/Y H:i', '10/02/2020 23:00');

$diff = $datetime1->diff($datetime2);
$days_ago = $diff->days;
$months_ago = $diff->m + ($diff->y * 12);
$hours_ago = $diff->h + ($diff->days * 24);

echo "{$days_ago} days ago"; // 365 days ago
echo "{$hours_ago} hours ago"; // 8776 hours ago
echo "{$months_ago} months ago"; // 12 months ago

4. Modify DateTime

$datetime = DateTime::createFromFormat('d/m/Y', '10/02/2020');

$datetime->modify('+1 day');
echo $datetime->format('d M Y'); // 11 Feb 2020

$datetime->modify('+2 day +1 month');
echo $datetime->format('d M Y'); // 14 Mar 2020

$datetime->modify('-10 day -2 month -1 year');
echo $datetime->format('d M Y')); // 04 Jan 2019

Conclusion

There are other methods, but I skip them because they’re doing the same thing like current_time().

All the above should cover most use cases in a WordPress site. Let us know in the comments if we are missing some important bits 🙂

Default image
Henner Setyono
A web developer who mainly build custom theme for WordPress since 2013. Young enough to never had to deal with using Table as layout.
Leave a Reply to zaranCancel Reply

3 Comments

  1. Thanks for sharing this info Henner Setyono. This is helpful.

  2. What about if you need to localize those dates based on the timezone in the WordPress settings? AFAIK DateTime can use a timezone string (New_York/America) but it cannot use an offset. Since WordPress gives the admin the option to set either in the WordPress settings, how do you account for either situation?

    • Hi Alex,

      current_datetime() gives you the DateTime of the WordPress setting. I just tested it by changing the setting.