Relative dates for posts and comments in WordPress

Calendar dates are nice but relative dates (i.e 1 day ago) can be more attractive. You may have noticed relative dates in Twitter and Facebook status updates. You can also display relative dates for posts and comments without using a WordPress plugin.

In your Theme’s functions.php file, add the following snippet:

// For posts
add_filter( 'get_the_date', 'human_get_the_date' ); 
// For comments
add_filter( 'get_comment_date', 'human_get_the_date' ); 
function human_get_the_date ($date) {
    return human_time_diff( strtotime( $date ) ) . ' ago';

The above snippet can also display relative time i.e 1 min ago. However, it cannot display relative dates in terms of months, years or centuries 😀

Leave a Reply