Skip to content

Instantly share code, notes, and snippets.

@dshafik
Created September 20, 2011 15:04
Show Gist options
  • Save dshafik/1229343 to your computer and use it in GitHub Desktop.
Save dshafik/1229343 to your computer and use it in GitHub Desktop.
Fuzzy Date Function
<?php
function fuzzyDate($date, $inputFormat = DateTime::ATOM, $outputDateFormat = "l, F dS, Y", $outputTimeFormat = "H:ia") {
if (!$inputFormat) {
$inputFormat = DateTime::ATOM;
}
if (!$outputDateFormat) {
$outputDateFormat = "l, F dS, Y";
}
$dateTime = DateTime::createFromFormat($inputFormat, $date);
// Failed to parse, probably invalid date
if (!$dateTime) {
return false;
}
// Get Timezone so we can use it for the other dates
$timezone = $dateTime->getTimeZone();
// Fuzzy Date ranges
$lastWeekStart = new DateTime("2 weeks ago sunday 11:59:59", $timezone);
$yesterdayStart = new DateTime("yesterday midnight");
$todayStart = new DateTime("today midnight", $timezone);
$todayEnd = new DateTime("today 23:59:59", $timezone);
$tomorrowStart = new DateTime("tomorrow midnight", $timezone);
$tomorrowEnd = new DateTime("tomorrow 23:59:59", $timezone);
$thisWeekStart = new DateTime("1 week ago sunday 11:59:59", $timezone);
$thisWeekEnd = new DateTime("sunday 11:59:59", $timezone);
$nextWeekEnd = new DateTime("1 week sunday midnight", $timezone);
$prefix = '';
// We have to start with the oldest onces first
if ($dateTime < $lastWeekStart) {
// Older than 1 week
$prefix = "on";
$fuzzyDate = ucwords($dateTime->format($outputDateFormat));
} elseif ($dateTime > $lastWeekStart && $dateTime < $thisWeekStart) {
// Some time in the previous week
$prefix = "Last";
$fuzzyDate = ucwords($dateTime->format("l"));
} elseif ($dateTime > $yesterdayStart && $dateTime < $todayStart) {
// Yesterday
$fuzzyDate = "Yesterday";
} elseif ($dateTime < $todayEnd) {
// Today
$fuzzyDate = "Today";
} elseif ($dateTime < $tomorrowEnd) {
// Tomorrow
$fuzzyDate = "Tomorrow";
} elseif ($dateTime < $thisWeekEnd) {
// Sometime in the current week
$prefix = "This";
$fuzzyDate = ucwords($dateTime->format("l"));
} elseif ($dateTime < $nextWeekEnd) {
// Some time in the following week
$prefix = "Next";
$fuzzyDate = ucwords($dateTime->format("l"));
} else {
// More than 2 weeks out.
$prefix = "on";
$fuzzyDate = ucwords($dateTime->format($outputDateFormat));
}
// Midnight or an actual time
if ($dateTime->format("Hi") != "0000") {
$fuzzyTime = $dateTime->format($outputTimeFormat);
} else {
$fuzzyTime = "midnight";
}
$format = '%s %s at %s';
return trim(sprintf($format, $prefix, $fuzzyDate, $fuzzyTime));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment