How to Change WordPress date format to time ago

June 7, 2019 0 comments
How to Change WordPress date format to time ago

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 a 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 the WordPress post date to time ago format and use it inside your WordPress themes.

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.

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' );
	// }else{
	// 	return the_time('F j, Y',$post->post_date);
	// }

That’s it! Just paste this snippet in your 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

Ashok kuikel

Hi, I am Ashok Kuikel, WordPress Developer for WordPress Community. While Cloud Computing Associate and Alibaba MVP and ACA for Cloud Professional.

You can follow me on Social Media, GitHub, and via my Blog Channels.

Leave a Reply

Articles and Tutorials

We love writing about WordPress and latest plugins tutorials, WooCommerce stats, and much more.