Я бы порекомендовал метод типа 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.