Pop Up Image

Get the Full PDF guide to creating an online store with WooCommerce (88 pages) for FREE

A comprehensive database for everything WordPress related.

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

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( 'get_the_date', '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

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!

Newsletter

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 (1)

  • Avatar
    ahmmed

    wow this post help me thanks

    reply

Leave a Reply