Created
May 24, 2017 15:38
-
-
Save henrytran9x/1b09c4d568c70db2f1b47cc3d282c03c to your computer and use it in GitHub Desktop.
Function Time Ago
This file contains hidden or 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
<?php | |
function timeAgo($time_ago) { | |
$time_ago = strtotime($time_ago) ? strtotime($time_ago) : $time_ago; | |
$time = time() - $time_ago; | |
switch($time): | |
// seconds | |
case $time <= 60; | |
return 'lessthan a minute ago'; | |
// minutes | |
case $time >= 60 && $time < 3600; | |
return (round($time/60) == 1) ? 'a minute' : round($time/60).' minutes ago'; | |
// hours | |
case $time >= 3600 && $time < 86400; | |
return (round($time/3600) == 1) ? 'a hour ago' : round($time/3600).' hours ago'; | |
// days | |
case $time >= 86400 && $time < 604800; | |
return (round($time/86400) == 1) ? 'a day ago' : round($time/86400).' days ago'; | |
// weeks | |
case $time >= 604800 && $time < 2600640; | |
return (round($time/604800) == 1) ? 'a week ago' : round($time/604800).' weeks ago'; | |
// months | |
case $time >= 2600640 && $time < 31207680; | |
return (round($time/2600640) == 1) ? 'a month ago' : round($time/2600640).' months ago'; | |
// years | |
case $time >= 31207680; | |
return (round($time/31207680) == 1) ? 'a year ago' : round($time/31207680).' years ago' ; | |
endswitch; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment