Skip to content

Instantly share code, notes, and snippets.

@gavinblair
Created April 12, 2011 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gavinblair/916500 to your computer and use it in GitHub Desktop.
Save gavinblair/916500 to your computer and use it in GitHub Desktop.
The user can input TO and FROM dates and times for an event. How do we display the event time nicely?
<?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
//"January 6th - 3:00pm - 6:00pm"
$return = date('F jS - g:ia', $starttime) . " to " . date('g:ia', $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;
}
@SeanJA
Copy link

SeanJA commented Apr 12, 2011

I suppose they make sense, you might want to make the second time optional though date_range('Jan 6', 'Jan 6') would be kind of annoying, also you called it daterange_range

@gavinblair
Copy link
Author

originially started as a custom Drupal module, hence the strange function name. Good idea to make second param optional.

@SeanJA
Copy link

SeanJA commented Apr 12, 2011

Ah, that makes sense... I agreen with @peterjanes I'd want date-time (e.g. "Jan. 6, 2010 3:00pm") rather than time-date.

@gavinblair
Copy link
Author

Going for readability, it's a good point to raise. "3:00pm on January 6th, 2010 to 6:00pm on February 12th, 2011" is kindof annoying no matter how you slice it.

@gavinblair
Copy link
Author

Untested, but here it is. Lots of IFs!

@gavinblair
Copy link
Author

Merged @SeanJA's updates and changed some of the formats to be more readable.

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