A comprehensive database for everything WordPress related.

WPKlik Logo Newsletter

Sign up and receive a free copy of How to Create an online Store with WooCommerce (full guide)

How to Display Publish Dates-as-Time-Ago

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.

Display Publish Dates

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.

Publish Dates

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:

Display Publish Dates as Time Ago

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:

Publish Dates

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:

date and time wordpress

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!


WordPress perfection at your fingertips.

If you enjoyed this article, feel free to subscribe to our newsletter using the form below. You can also follow us on Facebook and Twitter and subscribe to our YouTube channel for WordPress video tutorials.

Comments (13)

  • ahmmed

    wow this post help me thanks

  • Arman

    In my Newspaper theme its not working. It show 1 month ago for all posts

  • 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.

  • 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

  • tasvinder


    I am having trouble with this. I have added the code, but all that has happened is that the code as removed the date and has only left the time. The comments are from months ago and some new. but it only shows the time stamp. please help!

  • Vangel

    hey i am trying to do this on an elementor pro website and i don’t even have the editor option where can i find it. all i have in appliance is themes, customise and menus. thanks for the help this is very useful

  • Ivan


    tried to add the code, but no result – neither on posts or on links. No error messages. New site, latest WP, norwegian language.


Leave a Reply