Created
March 5, 2016 17:51
-
-
Save vivianspencer/4a995a117b786395f833 to your computer and use it in GitHub Desktop.
Verbose Beautified Date Range
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
/** | |
* Verbose Beautified Date Range | |
* | |
* @access public | |
* @param mixed $start_date | |
* @param mixed $end_date | |
* @return string $date_range (beautified date range) | |
* @license WTFPL | |
* | |
* @author Jon Brown <[email protected]> | |
* @since 1.0 | |
*/ | |
function my_verbose_date_range($start_date, $end_date) { | |
$date_range = ''; | |
// Setup basic dates | |
$start_date_pretty = date( 'F j', $start_date ); | |
$end_date_pretty = date( 'jS, Y', $end_date ); | |
// If years differ add suffix and year to start_date | |
if ( date('Y',$start_date) != date('Y',$end_date) ) { | |
$start_date_pretty .= date( 'S, Y', $start_date ); | |
} | |
// If months differ add suffix and year to end_date | |
if ( date('F',$start_date) != date('F',$end_date) ) { | |
$end_date_pretty = date( 'F ', $end_date) . $end_date_pretty; | |
} | |
// build date_range return string | |
if( ! empty( $start_date ) ) { | |
$date_range .= $start_date_pretty; | |
} | |
// check if there is an end date and append if not identical | |
if( ! empty( $end_date ) ) { | |
if( $end_date_pretty != $start_date_pretty ) { | |
$date_range .= ' - ' . $end_date_pretty; | |
} | |
} | |
return $date_range; | |
} | |
// Usage | |
echo my_verbose_date_range( strtotime('1/1/2016'), strtotime('2/1/2016') ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment