Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
You can’t perform that action at this time.