如果可以的话,我会推荐 MySQL TIME 类型的方法。不过,这是 PHP 中的一个方法,以备不时之需。
注意,它依赖于 24 小时制的时间,这意味着 22:00 是 10pm。此外,我不会将0:00 视为使用此代码的午夜,并且它不会跨越几天(即22:00 和5:00)。这只是简单的当日检查,它将时间转换为整数表示,然后将时间作为数字进行比较。
您还需要弄清楚如何获得自己的$schedules 数组设置。例如,您可以将其作为另一个参数传入。
编辑
一种基于 Regilero 使用的方法:
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
功能:
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;
}
测试用例:
$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
输出:
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.