-
-
Save SeanJA/917785 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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