Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Created June 7, 2013 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielhaim1/5731600 to your computer and use it in GitHub Desktop.
Save danielhaim1/5731600 to your computer and use it in GitHub Desktop.
Relative dates: - a moment ago - 1 min - 59 mins - 1 hour - 23 hours - 1 day - 7 days - 1 week - 5 weeks - 1 month - 11 months - 1 year - infinity years
<?php
if(!function_exists('how_long_ago')){
function how_long_ago($timestamp){
$difference = current_time('timestamp') - $timestamp;
if($difference >= 60*60*24*365){ // if more than a year ago
$int = intval($difference / (60*60*24*365));
$s = ($int > 1) ? 's' : '';
$r = $int . ' year' . $s . ' ago';
} elseif($difference >= 60*60*24*7*5){ // if more than five weeks ago
$int = intval($difference / (60*60*24*30));
$s = ($int > 1) ? 's' : '';
$r = $int . ' month' . $s . ' ago';
} elseif($difference >= 60*60*24*7){ // if more than a week ago
$int = intval($difference / (60*60*24*7));
$s = ($int > 1) ? 's' : '';
$r = $int . ' week' . $s . ' ago';
} elseif($difference >= 60*60*24){ // if more than a day ago
$int = intval($difference / (60*60*24));
$s = ($int > 1) ? 's' : '';
$r = $int . ' day' . $s . ' ago';
} elseif($difference >= 60*60){ // if more than an hour ago
$int = intval($difference / (60*60));
$s = ($int > 1) ? 's' : '';
$r = $int . ' hour' . $s . ' ago';
} elseif($difference >= 60){ // if more than a minute ago
$int = intval($difference / (60));
$s = ($int > 1) ? 's' : '';
$r = $int . ' minute' . $s . ' ago';
} else { // if less than a minute ago
$r = 'moments ago';
}
return $r;
}
}
/// if(!function_exists('how_long_ago')){the_time('F jS, Y'); } else { echo how_long_ago(get_the_time('U')); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment