Skip to content

Instantly share code, notes, and snippets.

@Stoltze
Created June 16, 2020 11:21
Show Gist options
  • Save Stoltze/8187f68499527f50329b7f5d02ecf971 to your computer and use it in GitHub Desktop.
Save Stoltze/8187f68499527f50329b7f5d02ecf971 to your computer and use it in GitHub Desktop.
A function that shows how long time is has been something. Ie. "4 minutes ago"
function TimeAgo ($oldTime, $newTime) {
$timeCalc = strtotime($newTime) - strtotime($oldTime);
if ($timeCalc >= (60*60*24*30*12*2)){
$timeCalc = intval($timeCalc/60/60/24/30/12) . " years ago";
}else if ($timeCalc >= (60*60*24*30*12)){
$timeCalc = intval($timeCalc/60/60/24/30/12) . " year ago";
}else if ($timeCalc >= (60*60*24*30*2)){
$timeCalc = intval($timeCalc/60/60/24/30) . " months ago";
}else if ($timeCalc >= (60*60*24*30)){
$timeCalc = intval($timeCalc/60/60/24/30) . " month ago";
}else if ($timeCalc >= (60*60*24*2)){
$timeCalc = intval($timeCalc/60/60/24) . " days ago";
}else if ($timeCalc >= (60*60*24)){
$timeCalc = " Yesterday";
}else if ($timeCalc >= (60*60*2)){
$timeCalc = intval($timeCalc/60/60) . " hours ago";
}else if ($timeCalc >= (60*60)){
$timeCalc = intval($timeCalc/60/60) . " hour ago";
}else if ($timeCalc >= 60*2){
$timeCalc = intval($timeCalc/60) . " minutes ago";
}else if ($timeCalc >= 60){
$timeCalc = intval($timeCalc/60) . " minute ago";
}else if ($timeCalc > 0){
$timeCalc .= " seconds ago";
}
return $timeCalc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment