2

I am looking for a way that I could add some code to my wordpress template to display the time of a post in relative terms. So if, for example, I posted something 5 minutes ago it would say something along the lines of Posted 6 minutes ago or 2 days ago, or 1 week ago. You get the idea. Could anyone give me some guidance on how I could go about doing this?

2 Answers 2

3

WordPress actually has an obscure and little known function called human_time_diff() that does this. It takes two arguments; the first is the earlier timestamp, and the second is the later timestamp. Both should be Unix timestamps. The first argument is required, but the second is optional, and will use time() if left empty. So, for example, in the loop, you could do this:

<p>Posted <?php echo human_time_diff( get_the_time( 'U' ) ); ?> ago.</p>

The function will only do minutes, hours, and days, though. If you need to get weeks, for example, you could do something like this:

$diff = explode( ' ', human_time_diff( get_the_time( 'U' ) ) );
if( $diff[1] == 'days' && 7 <= $diff[0] ){
  $diff[1] = 'week';
  $diff[0] = round( (int)$diff[0] / 7 );
  if( $diff[0] > 1 )
    $diff[1] .= 's';
  $diff = implode( ' ', $diff );
}

That would give you N week(s) as a string.

2
  • Wow, I was expecting it to be a lot more work than that. Then again I had no idea that human_time_diff() existed. Dec 14, 2010 at 19:35
  • 1
    Yeah, it took me a long time to find out about that function. Reading the source code, you'll uncover all sorts of cool gems like this, or the functions checked(), 'selected(), and disabled()` for checking, selecting, or disabling form elements. Dec 14, 2010 at 19:39
0

You can also try the time_since() function:

function time_since($older_date, $newer_date = false){
  $chunks = array(
    'year'   => 60 * 60 * 24 * 365,
    'month'  => 60 * 60 * 24 * 30,
    'week'   => 60 * 60 * 24 * 7,
    'day'    => 60 * 60 * 24,
    'hour'   => 60 * 60,
    'minute' => 60,
    'second' => 1
  );

  $newer_date = ($newer_date == false) ? (time()+(60*60*get_option("gmt_offset"))) : $newer_date;
  $since = $newer_date - $older_date;

  foreach ($chunks as $key => $seconds)
    if (($count = floor($since / $seconds)) != 0) break;

  $messages = array(
    'year'   => _n('%s year ago', '%s years ago', $count),
    'month'  => _n('%s month ago', '%s months ago', $count),
    'week'   => _n('%s week ago', '%s weeks ago', $count),
    'day'    => _n('%s day ago', '%s days ago', $count),
    'hour'   => _n('%s hour ago', '%s hours ago', $count),
    'minute' => _n('%s minute ago', '%s minutes ago', $count),
    'second' => _n('%s second ago', '%s seconds ago', $count),
  );
  return sprintf($messages[$key],$count);
}

call it like:

echo time_since(abs(strtotime($post->post_date." GMT"));

I think this is the original source of the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.