How to Display Publish Dates as Time Ago in WordPress

Wordpress618 Views

How to Display Publish Dates as “ Time Ago ” in WordPress . 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.

Time Ago

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:

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.

Leave a Reply

Your email address will not be published.

1 comment