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 | /* 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 | <?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 | 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.