Skip to content

Instantly share code, notes, and snippets.

@jordanandree
Created December 27, 2010 23:25
Show Gist options
  • Save jordanandree/756697 to your computer and use it in GitHub Desktop.
Save jordanandree/756697 to your computer and use it in GitHub Desktop.
<?php
function ago($time)
{
$time = strtotime($time);
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60", "60", "24", "7", "4.35", "12", "10");
$now = time();
$difference = $now - $time;
$tense = "ago";
for ($i = 0; $difference >= $lengths[$i] && $i < count($lengths) - 1; $i++) {
$difference /= $lengths[$i];
}
$difference = round($difference);
if ($difference != 1) {
$periods[$i] .= "s";
}
return "$difference $periods[$i] ago ";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment