WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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.

share|improve this answer
Wow, I was expecting it to be a lot more work than that. Then again I had no idea that human_time_diff() existed. – Adam Thompson Dec 14 '10 at 19:35
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. – John P Bloch Dec 14 '10 at 19:39

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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