Skip to content

Instantly share code, notes, and snippets.

@lean8086
Created May 22, 2012 19:34
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 lean8086/2771129 to your computer and use it in GitHub Desktop.
Save lean8086/2771129 to your computer and use it in GitHub Desktop.
Get a friendly date from unformated date
function friendlyDate($s) {
$minutes = (time() - strtotime($s)) / 60;
if ($minutes < 1) return "Seconds ago";
if ($minutes < 2) return "1 minute ago";
if ($minutes < 60) return floor($minutes)." minutes ago";
$hours = $minutes / 60;
if ($hours < 2) return "1 hour ago";
if ($hours < 24) return floor($hours)." hours ago";
$days = $hours / 24;
if ($days < 2) return "Yesterday";
if ($days < 30) return floor($days)." days ago";
$months = $days / 30;
if ($months < 2) return "1 month ago";
if ($months < 12) return floor($months)." months ago";
$years = $months / 12;
return ($years < 2) ? "1 year ago" : floor($years)." years ago";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment