简体   繁体   中英

How to get the days from the post date to the current date in WordPress

I have a wp website I am getting date of the post by the help of get_the_date()

I want it to show days instead date.

means if the post wast was made on "June 8, 2022" then it should show days how long its been posted of the post till the current date.

please help me to achive it. Thanks

The get_the_date() function should return a formatted date string, which you can pass in to PHP's DateTime class - see the docs .

This includes a couple of handy methods to allow you to retrieve past/future dates which could prove useful here, particularly DateTimeInterface::diff() which returns the difference as an instance of DateTimeInterval - here's how you might use it in your code:

$postedAt = new DateTime(get_the_date());
$postedAtDiff = $postedAt->diff(new DateTime());
$postedAtString = $postedAtDiff->format('%a days ago');

You can then echo out the $postedAtString where you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM