-
-
Save PunchRockgroin/851f70aedf5601a6d8603bafba3dc1f1 to your computer and use it in GitHub Desktop.
This file contains 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 the_date_range($args) { | |
global $post; | |
$default = array( | |
'start_field' => 'start_date', | |
'end_field' => null, | |
'base_format' => 'Ymd', | |
'post_id' => $post->ID, | |
'separator' => '<span class="date-separator">–</span>', | |
'month_format' => 'F', | |
'day_format' => 'j', | |
'year_format' => 'Y' | |
); | |
$s = array_intersect_key($args + $default, $default); | |
$start = get_field($s['start_field'], $s['post_id']); | |
$end = get_field($s['end_field'], $s['post_id']); | |
// Checks to make sure the start is a valid field | |
if($start) { | |
$raw_dates['start'] = DateTime::createFromFormat( $s['base_format'], $start ); | |
} else { | |
return; | |
} | |
// Adds end field if there is one | |
if( $end ) { | |
$raw_dates['end'] = DateTime::createFromFormat( $s['base_format'], $end ); | |
} | |
// Sets up the $dates array | |
foreach($raw_dates as $key => $value) { | |
$dates[$key] = array( | |
'month' =>$value->format($s['month_format']), | |
'day' => $value->format($s['day_format']), | |
'year' => $value->format($s['year_format']) | |
); | |
} | |
// if the years aren't the same the whole output has to change so we check that | |
// at the beginning | |
if($dates['start']['year'] == $dates['end']['year']) { | |
// if years are the same and months are the same | |
if($dates['start']['month'] == $dates['end']['month']) { | |
// if years, months and days are the same (same date in both fields) | |
if($dates['start']['day'] == $dates['end']['day']) { | |
$range = $dates['start']['month']." ".$dates['start']['day'].", ".$dates['start']['year']; | |
// if years and months are the same but not days | |
} else { | |
$range = $dates['start']['month']." ".$dates['start']['day'].$s['separator'].$dates['end']['day'].", ".$dates['start']['year']; | |
} | |
// if years are the same but months are not the same | |
} else { | |
$range = $dates['start']['month']." ".$dates['start']['day'].$s['separator'].$dates['end']['month']." ".$dates['end']['day'].", ".$dates['start']['year']; | |
} | |
} else { | |
$range = $dates['start']['month']." ".$dates['start']['day'].", ".$dates['start']['year'].$s['separator'].$dates['end']['month']." ".$dates['end']['day'].", ".$dates['end']['year']; | |
} | |
echo $range; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment