Skip to content

Instantly share code, notes, and snippets.

@dettmering
Created September 22, 2012 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dettmering/3767424 to your computer and use it in GitHub Desktop.
Save dettmering/3767424 to your computer and use it in GitHub Desktop.
PHP: Convert UNIX time into relative timestamp
// converts UNIX seconds into relative format
function relativeDate($date) {
$secondsago = time() - $date;
if ($secondsago <= 59) {$timestamp = "less than a minute ago";}
else if ($secondsago <= 119) {$timestamp = floor(($secondsago / 60)) . " minute ago (" . strftime('%R', $date) . ")";}
else if ($secondsago <= 3599) {$timestamp = round(($secondsago / 60)) . " minutes ago (" . strftime('%R', $date) . ")";}
else if ($secondsago <= 7199) {$timestamp = round(($secondsago / 3600),1) . " hours ago (" . strftime('%a %R', $date) . ")";}
else if ($secondsago <= 86400) {$timestamp = round(($secondsago / 3600)) . " hours ago (" . strftime('%a %R', $date) . ")";}
else if ($secondsago <= 345600) {$timestamp = round(($secondsago / 86400)) . " days ago (" . strftime('%a %R', $date) . ")";}
else { $timestamp = strftime('%d %m %Y %R', $date); }
return $timestamp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment