Display ‘Time Ago’ Into Your Post Date

31.05.10 by Jerome in Tutorials

Display ‘Time Ago’ Into Your Post Date

If you feel the need to jazz up your Blog post date why not try adding the “Time Ago” if your post is published within 24 hours. Thanks go to Lam Nguyen for this quick solution.

Simply drop this code blelow into your functions.php file

add_filter('the_time', 'timeago');
 
function timeago() {
    global $post;
    $date = $post->post_date;
    $time = get_post_time('G', true, $post);
    $time_diff = time() - $time;
    if ( $time_diff > 0 && $time_diff < 24*60*60 )
        $display = sprintf( __('%s ago'), human_time_diff( $time ) );
    else
        $display = date(get_option('date_format'), strtotime($date) );
 
    return $display;
}

Source

Related posts:

date, function, time

Get More Wordpress Tips & Tricks

Liked this post? subscribe to our email updates or RSS feed!

    blog comments powered by Disqus