How to Display Publish Dates as “Time Ago” Without Plugin

2022-11-17T17:50:02.000000Z byAdmin inWordPress
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(); ?>