Created
February 26, 2013 10:06
-
-
Save wpottier/5037420 to your computer and use it in GitHub Desktop.
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
public function relativeDate($date) | |
{ | |
$today = strtotime(date('M j, Y')); | |
if ($date instanceof \DateTime) { | |
$date = $date->getTimestamp(); | |
} | |
$reldays = ($date - $today) / 86400; | |
if ($reldays >= 0 && $reldays < 1) { | |
return 'today'; | |
} else if ($reldays >= 1 && $reldays < 2) { | |
return 'tomorrow'; | |
} else if ($reldays >= -1 && $reldays < 0) { | |
return 'yesterday'; | |
} | |
if (abs($reldays) < 7) { | |
if ($reldays > 0) { | |
$reldays = floor($reldays); | |
return 'in ' . $reldays . ' day' . ($reldays != 1 ? 's' : ''); | |
} else { | |
$reldays = abs(floor($reldays)); | |
return $reldays . ' day' . ($reldays != 1 ? 's' : '') . ' ago'; | |
} | |
} | |
if (abs($reldays) < 182) { | |
return date('l, j F', $date ? $date : time()); | |
} else { | |
return date('l, j F, Y', $date ? $date : time()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment