By default, WordPress offers you a nice solution to change your WordPress date format or time format in general settings of your admin dashboard. However, some people may prefer to have so-called “time ago” format in the similar way to Twitter and Facebook style like posted “10 minutes ago”, “1 hour ago”, “3 hours ago”, etc…
This may be quite useful for websites with lots of content publishing very frequently. But in some cases it is just a matter of your personal taste. In this tutorial, we are going to show you 3 ways of changing WordPress post date to time ago format and use it inside your WordPress themes.
1. Change WordPress date format to time ago using custom template function
In the code snippet below, you will see custom function mks_time_ago() which you can use in any template of your WordPress theme instead of WordPress native function like the_date() and the_time().
1 2 3 4 5 6 |
/* Function which displays your post date in time ago format */ function meks_time_ago() { return human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ).' '.__( 'ago' ); } |
Simple as that! We are just using WordPress current_time() and human_time_diff(). Just keep in mind to add this function somewhere in the functions.php file of your theme. Now here is a basic usage example of the function inside of a WordPress theme template file.
1 2 3 4 5 6 7 8 9 |
<?php if ( have_posts() ): ?> <?php while ( have_posts() ): the_post(); ?> <?php the_title(); /* post title */ ?> <?php echo meks_time_ago(); /* post date in time ago format */ ?> <?php the_content(); /* post content */ ?> <?php endwhille(); ?> <?php endif; ?> |
2. Using post time and date WordPress filter hooks
This solution is more suitable for those who don’t want to change the theme template files. In the following example we are using a callback function to override built in WordPress get_the_date and get_the_time functions and display time ago format automatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
add_filter( 'get_the_date', 'meks_convert_to_time_ago', 10, 1 ); //override date display add_filter( 'the_date', 'meks_convert_to_time_ago', 10, 1 ); //override date display add_filter( 'get_the_time', 'meks_convert_to_time_ago', 10, 1 ); //override time display add_filter( 'the_time', 'meks_convert_to_time_ago', 10, 1 ); //override time display /* Callback function for post time and date filter hooks */ function meks_convert_to_time_ago( $orig_time ) { global $post; $orig_time = strtotime( $post->post_date ); return human_time_diff( $orig_time, current_time( 'timestamp' ) ).' '.__( 'ago' ); } |
That’s it! Just paste this snippet in you theme functions.php file. Also, depending of your theme code and logic, you may only want to use time OR date filters so you can remove unnecessary hooks optionally.
3. Using Meks Time Ago WordPress plugin
For all of you who are not familiar with coding we have created a plugin which will change your post date format to time ago and should work with any theme out of the box. Meks Time Ago WordPress plugin is 100% free and can be downloaded from official WordPress plugin repository.
Once you install and activate the plugin you can go and manage its simple options. The sweetest thing here is the limitation option so you can apply time ago format only for posts which are not older than specific time frame. For example, let’s say that you want to show time ago date format only for posts which are published in the last 5 days while other posts will be displayed in standard date format.
Live example?
Yes, we are using this “time ago” approach in our several WordPress themes so you can see it live in action on our demo websites for Voice Theme, Throne Theme, and Sidewalk Theme. Feel free to try and let us know what do you think in comments section.
thank you a lot this article have helped me a lot.
you are the best 🙂
I’m so very glad to read that, Isaac, thank you for commenting! 🙂 And feel free to stop by again, plenty more of good stuff here!
Hey Ismail,
You can compare $orig_time and current_time( ‘timestamp’ ) values before returning the output. And if the difference is 1 day or 2 days, you can simply return your custom output string like Today, or Yesterday.
Hope this helps, cheers!
Hey,
How can i change to “Today” instead of hours ago ?
I want to show my posts something like that
Today,
Yesterday,
2 days ago,
1 week ago
Very informative – thanks a lot
I am really satisfied with this posting that you have given us. This is really nice work done by you. Thank you and looking for more posts.
Thanks Bojan,
I applied this on my two websites : https://amarverma.com and https://planobration.com
Thanks again.
Thanks Meks,
Its helpful for my blog
At 2nd example there are mistake – in add_filter () functions.
Hi,
Can you please point us out so we can edit? We haven’t noticed anything wrong.
Please where can I find the_date() code on the Meks Voice Theme. I need to do some modifications and make my date show the Last Updated Time because I am updating most of my posts and I will like my readers to know without actually putting it in the post. I have searched and searched please help
Hi John,
For all questions regarding our themes, please contact us via https://mekshq.com/contact and we would be glad to have a look and help.
Thanks!
I was searching this long time ago but not able to find it, but now i am tring to use it for my website http://educationafter12th.com/
Hi, I was searching for this long -time ago-, but I need to modify the “ago” word for “hace” because the site is in spanish, and the word “hace” goes before the time like this:
1 hour ago to:
Hace 1 hora (in spanish)
I really apreciate your valuable help for this little problem 🙂
Thank You!
Hi Vikash,
Thanks for your words, it is highly appreciated!