Created
April 20, 2016 09:48
-
-
Save YugalXD/4f86d30aa67fa87b169f711a6cfc8ebd to your computer and use it in GitHub Desktop.
Facebook like reletive time in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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