Skip to content

Instantly share code, notes, and snippets.

@peponi
Created August 22, 2012 13:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peponi/3425414 to your computer and use it in GitHub Desktop.
Save peponi/3425414 to your computer and use it in GitHub Desktop.
Convert UNIX timestamp to days hours minute seconds
<?php
function GetTimeDiff($timestamp)
{
$how_log_ago = '';
$seconds = time() - $timestamp;
$minutes = (int)($seconds / 60);
$hours = (int)($minutes / 60);
$days = (int)($hours / 24);
if ($days >= 1) {
$how_log_ago = $days . ' day' . ($days != 1 ? 's' : '');
} else if ($hours >= 1) {
$how_log_ago = $hours . ' hour' . ($hours != 1 ? 's' : '');
} else if ($minutes >= 1) {
$how_log_ago = $minutes . ' minute' . ($minutes != 1 ? 's' : '');
} else {
$how_log_ago = $seconds . ' second' . ($seconds != 1 ? 's' : '');
}
return $how_log_ago;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment