Like the article? Be sure to subscribe to our RSS feed and follow us on Twitter to stay up on recent content.

The completed code you need is below. Copy it and paste it to the functions.php file and you are ready to go. No more messing needed.

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;
}

This code will hook into the the_time() function. It will do the filter which modify the return value of the_time() before sending it to the browser screen.

The output:

Published:  5 minutes ago
Published:  12 hours ago

Or

Published:  March 14, 2010 //Depends on your date format setting

Don’t forget to subscribe to our rss feed to receive full code snippets and articles via feed reader or follow us on twitter.