Created
June 23, 2020 19:30
-
-
Save gabeshaughnessy/3b56966fd513e96d72d32cd747b9b9fd to your computer and use it in GitHub Desktop.
Example Date Time Formatting
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 | |
//Creates a formated string from a start and end date DateTime, a start and end time DateTime, and optional DateTimeZone. | |
function formatDateRange($d1, $d2, $t1, $t2, $tz=false) { | |
if(gettype($t1) === 'object' && gettype($t2) === 'object'){ | |
$formatted_time = ""; | |
if($tz && preg_match('/(\-|\+)[0-9]*/', $tz->getName() )){ | |
//use 24 hour time for EMEA | |
$hour_format = 'G'; | |
$ampm = false; | |
$time_zone = $tz->getName(); | |
$t1->display_minutes = ':i'; | |
$t2->display_minutes = ':i'; | |
} | |
else{ | |
$hour_format = 'g'; | |
$ampm = ' A'; | |
$time_zone = ($tz ? ' '.$tz->getName() : false); | |
$t1->display_minutes = ($t1->format('i') === '00' ? '' : ':i'); | |
$t2->display_minutes = ($t2->format('i') === '00' ? '' : ':i'); | |
} | |
if ($d1->format('Y-m-d') === $d2->format('Y-m-d')) { | |
//if it's the same day, show the time | |
if ($t1->format('h:i') === $t2->format('h:i')){ | |
//same hour and minute | |
$formatted_time = $t1->format('g'.$t1->display_minutes.' '); | |
}elseif($t1->format('h') === $t2->format('h')){ | |
//same hour of the day | |
$formatted_time = $t1->format($hour_format.$t1->display_minutes).' - '.$t2->format($hour_format.$t2->display_minutes.$ampm); | |
} | |
elseif($t1->format('a') === $t2->format('a')){ | |
//same meridian | |
$formatted_time = $t1->format($hour_format.$t1->display_minutes).' - '.$t2->format($hour_format.$t2->display_minutes.$ampm); | |
}else{ | |
$formatted_time = $t1->format($hour_format.$t1->display_minutes).' - '.$t2->format($hour_format.$t2->display_minutes.$ampm); | |
} | |
}else{ | |
//different_days - | |
$formatted_time = false; | |
} | |
} | |
if(gettype($d1) === 'object' && gettype($d2) === 'object'){ | |
if ($d1->format('Y-m-d') === $d2->format('Y-m-d')) { | |
# Same day | |
return $d1->format('M j, Y').(isset($formatted_time) ? ' | ' : '').$formatted_time.$time_zone; | |
} elseif ($d1->format('Y-m') === $d2->format('Y-m')) { | |
# Same calendar month | |
return $d1->format('M j') . $d2->format(' – j, Y'); | |
} elseif ($d1->format('Y') === $d2->format('Y')) { | |
# Same calendar year | |
return $d1->format('M j') . $d2->format(' – M j, Y'); | |
} else { | |
# General case (spans calendar years) | |
return $d1->format('M j, Y') . $d2->format(' – M j, Y'); | |
} | |
}else{ | |
return false; | |
} | |
} | |
//Example with start and end date and time ACF fields and a timezone acf field. | |
$dt_array = array( | |
'start_date' => DateTime::createFromFormat('F j, Y', get_field('start_date', $post_id)), | |
'end_date' => DateTime::createFromFormat('F j, Y', get_field('end_date', $post_id)), | |
'start_time' => DateTime::createFromFormat('g:i a', get_field('start_time', $post_id)), | |
'end_time' => DateTime::createFromFormat('g:i a', get_field('end_time', $post_id)) | |
); | |
if(get_field('time_zone', $post_id)){ | |
$tz = new DateTimeZone( get_field('time_zone', $post_id)); | |
}else{ | |
$tz = ''; | |
} | |
$date_str = formatDateRange($dt_array['start_date'], $dt_array['end_date'], $dt_array['start_time'], $dt_array['end_time'], $tz); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment