Skip to content

Instantly share code, notes, and snippets.

@YugalXD
Created April 20, 2016 09:48
Show Gist options
  • Save YugalXD/4f86d30aa67fa87b169f711a6cfc8ebd to your computer and use it in GitHub Desktop.
Save YugalXD/4f86d30aa67fa87b169f711a6cfc8ebd to your computer and use it in GitHub Desktop.
Facebook like reletive time in PHP
function getmychattime($datetime){
$timestamp = strtotime($datetime);
$now = time();
$diff = $now - $timestamp;
if ($diff < 60){
return date('g:ia',$timestamp);
}
$diff = floor($diff/60);
if ($diff < 60){
return date('g:ia',$timestamp);
}
$diff = floor($diff/60);
if ($diff < 24){
return date('g:ia',$timestamp);
}
$diff = floor($diff/24);
if ($diff < 7){
return date('D',$timestamp);
}
if ($diff < 30)
{
return date('d/m',$timestamp);
}
$diff = floor($diff/30);
if ($diff < 12){
return date('d/m',$timestamp);
}
$diff = date('Y', $now) - date('Y', $date);
return sprintf($diff > 1 ? '%s years ago' : 'last year', $diff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment