Skip to content

Instantly share code, notes, and snippets.

@aembleton
Created April 15, 2015 12:02
Show Gist options
  • Save aembleton/6f7fbcb63f66f76a8713 to your computer and use it in GitHub Desktop.
Save aembleton/6f7fbcb63f66f76a8713 to your computer and use it in GitHub Desktop.
PHP function to create a date range such as '1st - 4th May 2015' or '28th August - 2nd September 2015'
function getPrettyDateRange($fromDate, $toDate) {
$fromTime = strtotime($fromDate);
$toTime = strtotime($toDate);
$fromDay = date("jS", $fromTime);
$fromMonth = date("F", $fromTime);
$fromYear = date("Y", $fromTime);
$toDay = date("jS", $toTime);
$toMonth = date("F", $toTime);
$toYear = date("Y", $toTime);
$toDatePretty = $toDay." ".$toMonth." ".$toYear;
if ($fromYear != $toYear) {
return $fromDay." ".$fromMonth." ".$fromYear." - ".$toDatePretty;
}
if ($fromMonth != $toMonth) {
return $fromDay." ".$fromMonth." - ".$toDatePretty;
}
if ($fromDay != $toDay) {
return $fromDay." - ".$toDatePretty;
}
return $toDatePretty;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment