How to Display Publish Dates as “Time Ago” Without Plugin
For Post: Go to wp-content/your-theme/functions.php file and scroll all the way down.
Insert this hook:
// Calclute how many time ago posted
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');
Use this function anywhere in your template:
<?php echo my_post_time_ago_function(); ?>
If you want same thing to apply for comment dates, create a similar function:
Insert this hook:
// Calclute how many time ago Commented
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' );
Use this function anywhere in your template:
<?php echo my_comment_time_ago_function(); ?>3 comments
MH
Mahmud Hossain
July 16, 2026
Well written. Bookmarked this for my team's reference. Looking forward to more content like this.
SB
Sumaiya Begum
July 15, 2026
Does this approach also work for larger datasets? Our team is dealing with something similar at scale.
RK
Rafiq Khan
July 14, 2026
Really insightful article! This is one of the clearest explanations I've come across. The examples make it very easy to understand.
AD
AdminAdminJuly 14, 2026Thank you Rafiq! Glad it was helpful.
