The author developed a plugin related to timetables in PHP and WordPress, but found the many time methods overwhelming. They share common DateTime formats and how to work with DateTime objects in WordPress, including getting the current date, converting from a string, and modifying DateTime objects. They also explain how to calculate the interval between DateTimes and provide examples for modifying DateTime objects by a certain number of days or months. The article aims to help those working with time-related themes or plugins in WordPress and encourages readers to share if they found the information useful.
A year ago, I developed a plugin related to timetable. And I soon realized that there are many time methods in PHP. Combined with the built-in WordPress functions, I was overwhelmed.
But fret not! Today, I will show you the best ways to work with date time objects in WordPress.
DateTime Format
The table below shows the common DateTime formats. You can find the full list in the PHP documentation.
- Year:
- Month:
M
: Jan – Decm
: 01 – 12F
: January – December
- Day:
D
: Mon – Sund
: 01 – 31
- Hour:
H
: 00 – 23h
: 01 – 12A
: AM or PM
- Minute:
- Second:
DateTime Object
Current date
$current_date = current_datetime();
echo $current_date->format( 'd/m/Y' );
// if you want to use the format based on settings
echo $current_date->format( get_option('date_format') );
By post ID
$post_date = get_post_datetime( $post_id );
echo $post_date->format( 'd/m/Y' );
Convert from a string
$meta_date="13/01/2020";
$datetime = DateTime::createFromFormat( 'd/m/Y', $meta_date );
echo $datetime->format( 'd M Y' ); // 13 Jan 2020
The publication date of Posts and Comments is stored in the format Y-m-d H:i:s
. If the string is in that format, you can use the code below:
$post_date="2020-01-16 12:00:00";
$datetime = new DateTime( $post_date );
echo $datetime->format( 'd M Y' ); // 16 Jan 2020
Interval between DateTimes
If you want to know the distance between 2 DateTimes in months, days, hours, you can refer to the code below:
$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
Modify DateTime
In case you want to change a DateTime, maybe increase or decrease by a certain number of days or months, you can use the code below:
$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
I hope this article will help you, especially if you’re working with time-related themes or plugins in WordPress. If you found this article useful and it saved you time, please consider sharing it. Also, if you’re interested in similar topics, read other WordPress Tips articles and follow our Fanpage so you don’t miss new posts.