Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Forked from gavinblair/daterange.php
Created April 13, 2011 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanJA/917785 to your computer and use it in GitHub Desktop.
Save SeanJA/917785 to your computer and use it in GitHub Desktop.
<?php
function daterange_range($starttime, $endtime = null) {
$return = "";
//set the endtime to the start time if there isn't an endtime provided
$endtime = empty($endtime) ? $starttime : $endtime;
//turn them into timestamps if they are not timestamps already
$endtime = ((int)$endtime == $endtime && is_int($endtime))? $endtime:strtotime($endtime);
$starttime = ((int)$starttime == $starttime && is_int($starttime))? $starttime:strtotime($starttime);
//boolean values to prevent test duplication,
//better names could possibly be chosen
$same_time = ($starttime == $endtime);
$time_present = daterange_timespresent($starttime, $endtime);
$year_same = (date('Y', $starttime) == date('Y', $endtime));
$month_same = (date('m', $starttime) == date('m', $endtime));
$day_same = (date('d', $starttime) == date('d', $endtime));
$time_same = (date('H:i', $starttime) == date('H:i', $endtime));
//starttime and endtime are the same
if ($same_time) {
$return = date('F jS', $starttime);
//time?
if (date('g:ia', $starttime) != "12:00am") {
$return .= date(' g:ia', $starttime);
}
} else {
if (!$year_same && !$month_same && $time_present) {
//different years, different months, times present
//"November 6th 3:00pm, 2010 to February 12th 6:00pm, 2011"
$return = date('F jS g:ia, Y', $starttime) . " to " . date('F jS g:ia, Y', $endtime);
} else if (!$year_same && !$month_same && !$time_present) {
//different years, different months, no times
//"November 6th, 2010 to February 12th, 2011"
$return = date('F jS, Y', $starttime) . " to " . date('F jS, Y', $endtime);
} elseif ($year_same && !$month_same && $time_present) {
//different months, times present
//"January 6th 3:00pm to February 12th 6:00pm"
$return = date('F jS g:ia', $starttime) . " to " . date('F jS g:ia', $endtime);
} elseif ($year_same && !$month_same && !$time_present) {
//different months, no times
//"January 6th to February 12th"
$return = date('F jS', $starttime) . " to " . date('F jS', $endtime);
} elseif ($year_same && $month_same && !$day_same && $time_present) {
//same month, different days, times present
//"January 6th 3:00pm to January 12th 6:00pm"
$return = date('F jS g:ia', $starttime) . " to " . date('F jS g:ia', $endtime);
} elseif ($year_same && $month_same && !$day_same && !$time_present) {
//same month, different days, no times
//"January 6th to 12th"
$return = date('F jS', $starttime) . " to " . date('jS', $endtime);
} elseif ($year_same && $month_same && $day_same && $time_present) {
//times present, same day
//"3:00pm - 6:00pm on January 6th"
$return = date('g:ia', $starttime) . " - " . date('g:ia \o\n F jS', $endtime);
} elseif ($year_same && $month_same && $day_same && !$time_present) {
//same day, no times
//"January 6th"
$return = date('F jS');
}
}
if (($year_same) && date('Y', $starttime) != date('Y')) {
//Both the same year (or endtime is null), but not this year? Append the year
//", 2012"
$return .= date(', Y', $starttime);
}
return $return;
}
function daterange_timespresent($starttime, $endtime) {
if (date('g:ia', $starttime) == "12:00am" && date('g:ia', $endtime) == "12:00am") {
return false;
}
return true;
}
@gavinblair
Copy link

//turn them into timestamps -- this function is meant to take two timestamps. Is there a down-side to doing strtotime() on something that is already a timestamp?

@SeanJA
Copy link
Author

SeanJA commented Apr 13, 2011

I have changed that, if the value looks like a time stamp (the intval == the string val and it is an int) then it ignores it, otherwise it tries to convert it to a timestamp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment