I would recommend the MySQL TIME
type method if you can. Here is a method in PHP, though, in case you need it.
Note, it relies on time being in the 24 hour clock, which means that 22:00
is 10pm
. Also, I'm not handling 0:00
as being midnight with this code, and it doesn't span across days (ie, 22:00
and 5:00
). This is simply a plain, same-day check, and it converts the times into integer representations and then compares the times as numbers.
You will also need to figure out how you're going to get your own $schedules
array setup. You could pass it in as another argument, for instance.
EDIT
An approach based on the one Regilero uses:
function checkNewTime($day, $start, $end, &$error) {
$_start = str_replace(':', '', $start);
$_end = str_replace(':', '', $end);
$error = '';
$schedules = array(
'Monday' => array(
array('1:00', '3:00'),
array('6:00', '7:00'),
array('8:00', '10:00')
)
);
if ($_end <= $_start) {
$error = "The new start time ($start) cannot be after the end time ($end).";
return false;
}
$schedules = $schedules[$day];
$c_schedules = count($schedules);
for ($i = 0; $i < $c_schedules; $i++) {
$interval = "{$schedules[$i][0]} and {$schedules[$i][1]}";
$interval_start = str_replace(':', '', $schedules[$i][0]);
$interval_end = str_replace(':', '', $schedules[$i][1]);
if ($_start < $interval_end && $_end > $interval_start) {
$error = "The start is between schedules time $interval.";
return false;
}
}
return true;
}
http://codepad.org/hznpATft
The function:
function checkNewTime($day, $start, $end, &$error) {
$_start = str_replace(':', '', $start);
$_end = str_replace(':', '', $end);
$error = '';
$schedules = array(
'Monday' => array(
array('1:00', '3:00'),
array('6:00', '8:00'),
array('8:00', '10:00')
)
);
if ($_end <= $_start) {
$error = "The new start time ($start) cannot be after the end time ($end).";
return false;
}
$schedules = $schedules[$day];
$c_schedules = count($schedules);
for ($i = 0; $i < $c_schedules; $i++) {
$interval = "{$schedules[$i][0]} and {$schedules[$i][1]}";
$interval_start = str_replace(':', '', $schedules[$i][0]);
$interval_end = str_replace(':', '', $schedules[$i][1]);
if ($_start > $interval_start && $_start < $interval_end) {
$error = "The start is between schedules time $interval.";
}
if ($_end < $interval_end && $_end > $interval_start) {
$error .= " The end is between schedule times $interval.";
}
if ($_start < $interval_start && $_end > $interval_end) {
$error .= " The schedule encapsulates the schedule times $interval.";
}
if ($error != '') return false;
}
return true;
}
The testcase:
$times = array(
array('5:00','8:00'),
array('3:30','6:00'),
array('4:30','5:00'),
array('5:30','7:00'),
array('9:00','10:00'),
array('9:30','11:00'),
array('21:30','12:00'),
array('11:30','11:30'),
array('12:30','20:00'),
array('15:30','18:00'),
array('12:30','19:00'),
array('19:30','24:00'),
array('17:30','23:45')
);
$c_times = count($times);
$error = '';
for ($i = 0; $i < $c_times; $i++) {
if (checkNewTime('Monday', $times[$i][0], $times[$i][1], $error)) {
echo "{$times[$i][0]}, {$times[$i][1]} does not conflict.\n\n";
} else {
echo "{$times[$i][0]}, {$times[$i][1]} does conflict.\nError: $error\n\n";
}
}
http://codepad.org/pEq9Wdh4
Outputs:
5:00, 8:00 does conflict.
Error: The schedule encapsulates the schedule times 6:00 and 7:00.
3:30, 6:00 does not conflict.
4:30, 5:00 does not conflict.
5:30, 7:00 does conflict.
Error: The end is between schedule times 6:00 and 8:00.
9:00, 10:00 does conflict.
Error: The start is between schedules time 8:00 and 10:00.
9:30, 11:00 does conflict.
Error: The start is between schedules time 8:00 and 10:00.
21:30, 12:00 does conflict.
Error: The new start time (21:30) cannot be after the end time (12:00).
11:30, 11:30 does conflict.
Error: The new start time (11:30) cannot be after the end time (11:30).
12:30, 20:00 does not conflict.
15:30, 18:00 does not conflict.
12:30, 19:00 does not conflict.
19:30, 24:00 does not conflict.
17:30, 23:45 does not conflict.