Hack: Use Time Ago Instead of Dates and Times For Comments

Having the date and time next to each comment on your site works fine. But social media networks have conditioned everyone to look at “time ago” indicators instead, like “3 minutes ago” and “2 days ago” and such. You can get these in your WordPress comments easily with an addition to your functions.php file.

add_filter('get_comment_date', 'wpfilter_time_ago', 10, 1);
add_filter('get_comment_time', 'wpfilter_time_ago', 10, 1);
 
function wpfilter_time_ago($orig_time) {
	global $post;
	$orig_time = strtotime($orig_time); 
	return human_time_diff($orig_time, current_time('timestamp')).' ago';
}

The human_time_diff function is actually part of WordPress and is probably used already by a number of themes. But these filters will replace the standard time stamp used to display comments. You can also use the get_the_date, the_date, get_the_time and the_time filter hooks to use time ago in more places on your site.

Leave a Reply

Your email address will not be published. Required fields are marked *