Last active
December 18, 2022 22:12
-
-
Save Snaver/f39e43ff258562d6576d to your computer and use it in GitHub Desktop.
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
public function humanizeDateDifference($now,$otherDate=null,$offset=null) | |
{ | |
if($otherDate != null){ | |
$offset = $now - $otherDate; | |
} | |
if ($offset == 0) | |
{ | |
$otherDate =$otherDate + 1; | |
$offset = $now - $otherDate; | |
} | |
if($offset != null){ | |
$deltaS = $offset%60; | |
$offset /= 60; | |
$deltaM = $offset%60; | |
$offset /= 60; | |
$deltaH = $offset%24; | |
$offset /= 24; | |
$deltaD = ($offset > 1)?ceil($offset):$offset; | |
} else{ | |
throw new Exception("Must supply otherdate or offset (from now)"); | |
} | |
if($deltaD > 1){ | |
if($deltaD > 365){ | |
$years = ceil($deltaD/365); | |
if($years ==1){ | |
return "last year"; | |
} else{ | |
return "<br>$years years ago"; | |
} | |
} | |
if($deltaD > 6){ | |
//return date('dS F Y',strtotime("$deltaD days ago")); | |
} | |
return "$deltaD days ago"; | |
} | |
if($deltaD == 1){ | |
return "Yesterday"; | |
} | |
if($deltaH == 1){ | |
return "last hour"; | |
} | |
if($deltaM == 1){ | |
return "1 minute ago"; | |
} | |
if($deltaH > 0){ | |
return $deltaH." hours ago"; | |
} | |
if($deltaM > 0){ | |
return $deltaM." minutes ago"; | |
} | |
else{ | |
return "a few seconds ago"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment