Skip to content

Instantly share code, notes, and snippets.

@Snaver
Last active December 18, 2022 22:12
Show Gist options
  • Save Snaver/f39e43ff258562d6576d to your computer and use it in GitHub Desktop.
Save Snaver/f39e43ff258562d6576d to your computer and use it in GitHub Desktop.
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