Skip to content

Instantly share code, notes, and snippets.

@danott
Created June 14, 2011 15:56
Show Gist options
  • Save danott/1025200 to your computer and use it in GitHub Desktop.
Save danott/1025200 to your computer and use it in GitHub Desktop.
Twitter style relative time.
<?php
/*
Origin: http://snipplr.com/view/4912/relative-time/
Copied here for my reference
*/
function plural($num) {
if ($num != 1)
return "s";
}
function getRelativeTime($date) {
$diff = time() - strtotime($date);
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment