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.
Year | Y y | 2015 15 |
Month | M m n F | Jan – Dec 01 – 12 1 – 12 January – December |
Day | D d j | Mon – Sun 01 – 31 1 – 31 |
Hour | H h A | 00 – 23 01 – 12 AM or PM |
Minute | i | 01 – 59 |
Second | s | 01 – 59 |
Timestamp | U | Amount of seconds since Jan 1970 |
2. Getting DateTime Object
CURRENT / TODAY DATE:
echo current_time('d/m/Y');
echo current_time('mysql'); // 'mysql' is shortcut of 'Y-m-d H:i:s'
echo current_time('mysql', true); // in GMT timezone
// get DateTime object
$current_dt = current_datetime();
echo $current_dt->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
Notes: The DateTime you get from current_datetime()
can’t be modified. So you need to workaround like this:
$dt = new DateTime(current_time('mysql'));
$dt->modify('+30 day');
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 🙂
Thanks for sharing this info Henner Setyono. This is helpful.
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.