Created
July 27, 2013 02:37
-
-
Save lgedeon/6093433 to your computer and use it in GitHub Desktop.
partial time range solution
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
/** | |
* Returns a compact date range like March 27-29, 1977 from two unix time stamps. If $end is empty | |
* or less than start returns start only. | |
* | |
* @param int $start Unix timestamp | |
* @param int $end Unix timestamp | |
* @param string $round Least unit of time to include - supported values: day, hour, minute | |
* @param string $include Largest unit of time to include - supported values: year, month, hour | |
* | |
* Samples: | |
* January 1, 2001 - January 1, 2002 | |
* January 1 - Februray 1, 2001 | |
* January 1 - 2, 2001 | |
* January 1, 2001 | |
* | |
* January 1, 2001 at 1pm - January 1, 2002 at 1pm | |
* January 1, 2001 at 1pm - Februray 1, 2001 at 1pm | |
* January 2001, 1st 1pm - 2nd 2pm | |
* January 1, 2001 1pm - 2pm | |
* | |
* | |
* @return string | |
*/ | |
function human_time_range ( $start, $end, $round = 'day', $include = 'year' ) { | |
$y = ( 'year' == $include ) ? ', Y' : ''; | |
$m = ( in_array( $include, array( 'month', 'year' ) ) ) ? 'F ' : ''; | |
$d = ( 'hour' != $include ) ? 'j' : ''; | |
if ($end != '' && $start < $end) { | |
$year1 = date( 'Y', $start ); | |
$year2 = date( 'Y', $end ); | |
$month1 = date( 'm', $start ); | |
$month2 = date( 'm', $end ); | |
$day1 = date( 'd', $start ); | |
$day2 = date( 'd', $end ); | |
$hour1 = date( 'H', $start ); | |
$hour2 = date( 'H', $end ); | |
if ( $year1 != $year2 ) { | |
return date('F j, Y', $start) . ' – ' . date('F j, Y', $end); | |
} elseif ( $month1 != $month2 ) { | |
return date('F j', $start) . '–' . date('F j', $end) . date(', Y', $end); | |
} elseif ( $day1 != $day2 ) { | |
return date('F j', $start) . '–' . date('j', $end) . date(', Y', $end); | |
} | |
} | |
return date('F j, Y', $start); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment