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
tasvinder
Hello,
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!
Jovan Kitanovic
Hi, thanks for reaching out.
Since you haven’t specified and this article contains several code options, we’re not sure which one you used. However, if you want to display both the date and the time, the code you’d need to use is the last one:
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’ );
Hope this answers your question!
Best regards,
Jovan
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
Jovan Kitanovic
Hi Vangel, thanks for writing in!
If you can’t edit the file directly from your dashboard, there’s a simple workaround—you can use FTP. You’ll need a couple of things for this:
FTP credentials (if you don’t have them already, you can make them or you can ask your hosting provider to create them for you),
and an FTP client (such as FileZilla, which is free).
Then you need to connect to your server using your chosen FTP client, find the file you need, download and edit it. Once that’s done, upload the edited file back to the same location to overwrite its older version. And you’re done!
If you’d like to see this broken down into steps, or if you’d like to have a go at making the FTP credentials yourself, you can check out this handy article: https://qodeinteractive.com/magazine/how-to-use-ftp/
Hope this helps!
Best regards,
Jovan
Ivan
Hi,
tried to add the code, but no result – neither on posts or on links. No error messages. New site, latest WP, norwegian language.
Jovan Kitanovic
Hi Ivan, thanks for writing in!
From the sound of things, you might be using a WordPress theme that employs a custom function or a custom hook to show the date and time of your published posts. This is nothing bad! It simply means the theme authors used something specific that our code (which is generalized) can’t properly integrate with. To get a bit more technical on you, hooking the code we provided to the default WordPress “the_time” hook won’t work if your theme doesn’t employ that hook already.
What this means is that you need to edit the code we provided to suit your theme. It’s not as scary as it may sound! You simply need to replace “the_time” within the code with a hook that is specific to your theme. You can find out what the theme-appropriate hook is by asking your theme authors.
Alternatively, you can insert a function call:
altered_post_time_ago_function ()
within the appropriate template file. To find out what the appropriate template file is for you, you’ll need to ask the authors of your theme.
Hope this helps!
Best regards,
Jovan