Last active
December 30, 2015 05:58
-
-
Save PhilKershaw/7785699 to your computer and use it in GitHub Desktop.
Showing call-back times for the whole week: Mon - Fri: 9am - 6pm
Sat: 9am -5:30pm
Sun: 11am - 5pm ...and breathe!
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 | |
$available_times = array( | |
"09:00:00" => '<option value="%s 09:00:00">%s 9.00am - 10.00am</option>', | |
"10:00:00" => '<option value="%s 10:00:00">%s 10.00am - 11.00am</option>', | |
"11:00:00" => '<option value="%s 11:00:00">%s 11.00am - 12.00pm</option>', | |
"12:00:00" => '<option value="%s 12:00:00">%s 12.00pm - 1.00pm</option>', | |
"13:00:00" => '<option value="%s 13:00:00">%s 1.00pm - 2.00pm</option>', | |
"14:00:00" => '<option value="%s 14:00:00">%s 2.00pm - 3.00pm</option>', | |
"15:00:00" => '<option value="%s 15:00:00">%s 3.00pm - 4.00pm</option>', | |
"16:00:00" => '<option value="%s 16:00:00">%s 4.00pm - 5.00pm</option>', | |
"17:00:00" => '<option value="%s 17:00:00">%s 5.00pm - %s</option>', | |
); | |
$current_time = time(); | |
$day_of_week = date('N'); | |
$end_time = '6:00pm'; | |
switch ($day_of_week) { | |
case 6: //Saturday | |
$end_time = '5:30pm'; | |
$start_slice_index = 0; | |
if ($current_time >= strtotime('09:00:00')) { | |
$start_slice_index = ($current_time >= strtotime('10:00:00')) ? 2 : 1; | |
} | |
$end_slice_index = ($current_time >= strtotime('17:00:00')) ? 6 : NULL; | |
$available_times = array_slice($available_times, $start_slice_index, $end_slice_index, TRUE); | |
break; | |
case 7: //Sunday | |
$sunday_times = &array_slice($available_times, 0, 2); | |
$sunday_times[] = &$available_times['17:00:00']; | |
foreach ($sunday_times as &$time) { | |
$time = sprintf( | |
$time, | |
'%s', | |
'Tomorrow', | |
$end_time | |
); | |
} | |
unset($time); | |
break; | |
default: | |
// do nothing | |
} | |
foreach ($available_times as $time => &$display) { | |
if ($current_time > strtotime($time) || ($day_of_week == 7 && $time == "17:00:00") ) { | |
$date = new DateTime(date('Y-m-d')); | |
$date = $date->modify('+1 day') | |
->format('Y-m-d'); | |
$day = 'Tomorrow'; | |
} else { | |
$date = date('Y-m-d'); | |
$day = 'Today'; | |
} | |
$display = sprintf( | |
$display, | |
$date, | |
$day, | |
$end_time | |
); | |
} | |
unset($display); | |
asort($available_times); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment