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

July 27, 2023 by Frederick in Web development
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(); ?>

Leave a comment


Name

Email


Website


Messagge



Comments

Federico Schiocchet 25 feb, 2018

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea coSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium

Jessica Albuzi 05 Dec, 2016

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea coSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium

Don Jhon 21 feb, 2016

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea coSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium

Sara Molin 14 Mar, 2016

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea coSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium

Vaky Yo 11 feb, 2017

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea coSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium