
How to Display Publish Dates as “Time Ago”
The way we display date and time on the Web doesn’t leave a lot of room for customization. There are only so many ways to present these values.
For instance, you have already probably noticed the format in which WordPress outputs the date of publishing for blog posts.
Usually, there’s a default value for the date format. Changing the time and date format is rather simple. It basically involves going to Settings > General > Date Format and picking your preferred format.
However, in this article, we’re particularly interested in the “Some time ago” and its format. The regular timestamp shows the date and/or time of publishing. The “time ago” displays how long ago we published a post.
If you are a Facebook or a Twitter user, you’re probably familiar with this format, which looks like this:
In this tutorial, we’re going to show you how to modify this default behavior the easy way. This involves adding a bit of code to your functions.php file. This code will create date displays for your posts and it will change depending on when the post was published.
Go to wp-content/your-theme/functions.php file and scroll all the way down.
Insert this hook:
function my_post_time_ago_function() { return sprintf( esc_html__( '%s ago', 'textdomain' ), human_time_diff(get_the_time ( 'U' ), current_time( 'timestamp' ) ) ); } add_filter( 'the_time', 'my_post_time_ago_function' );
Your end result should look like this:
If you want same thing to apply for comment dates, create a similar function:
function my_comment_time_ago_function() { return sprintf( esc_html__( '%s ago', 'textdomain' ), human_time_diff(get_comment_time ( 'U' ), current_time( 'timestamp' ) ) ); } add_filter( 'get_comment_date', 'my_comment_time_ago_function' );
And here’s the result:
Another thing that you can do is to combine the time ago method and full publish date. If your post is up to a week old, the date display will say that it was published X time ago. This can be 30 minutes ago or 5 days ago. And for posts that are older than a week, the code will display the full publish date. For example, Nov 16, 2020.
You can add the code by going to wp-content/your-theme/functions.php file and scrolling to the bottom. Underneath the code that’s already there, simply insert:
function altered_post_time_ago_function() { return ( get_the_time('U') >= strtotime('-1 week') ) ? sprintf( esc_html__( '%s ago', 'textdomain' ), human_time_diff( get_the_time ( 'U' ), current_time( 'timestamp' ) ) ) : get_the_date(); } add_filter( 'the_time', 'altered_post_time_ago_function' );
The “time ago” format is ideal for websites that post regular updates and have a generally very dynamic posting rhythm. Regular datestamps, which display date and time of publishing, are just fine, of course. But they don’t convey that feeling of immediacy and dynamics as the “Posted 15 minutes ago” or “Posted one day ago” does. Readers love being in the loop with the lighting-speed way the Internet works. Displaying how long ago a post was published is an easy yet efficient way to keep your visitors interested in your content.
We hope that you found this article to be helpful. If you liked it, please feel free to check out some of these articles as well!
ahmmed
wow this post help me thanks
Arman
In my Newspaper theme its not working. It show 1 month ago for all posts
Jovan Kitanovic
Hi, thanks for writing in!
These timestamps display the “time ago” value for your post in terms of days (for example, “2 days ago,” “10 days ago”, and so on) and whole months, not fractions of months. So, if your article is 48 days old, it will simply say “one month ago,” if it is 65 days old it will say “two months ago” and so on.
Your articles most likely just fall into the “one month” timeframe. You can try publishing a new post and see how it goes. You can also check out this article for further clarification of how it works.
Best regards,
Jovan
Eromi
hello sir. I m using refined magazine by candid, when I did it, it changes the display format instead of showing date and time, it only shows the time by hours counting, before the theme shows date in this format month, and day, and year old but now it only shows time in hours.
Jovan Kitanovic
Hi, thankas for writing in!
We checked your website, everything seems to be in perfect order. Your articles were just new, that’s why the time was displayed in hours.
Let us know if you need help with anything else.
Best regards,
Jovan
Jessica
Hi Jovan,
Great article. Question: Can you provide a hook that has this logic?
– Display Time ago if post is 7 days old or less.
– However, display full date for posts that are older than 7 days (i.e. Nov 16, 2020).
I’d like to combine the two. This way, people will see ‘time ago’ for posts <7 days old, and full dates for older posts.
Thank you
Jovan Kitanovic
Hi Jessica, thanks for writing in!
We have actually updated our article with precisely what you’re looking for, check it out, and let us know if we can help with anything else.
Best regards,
Jovan