如何检查范围内的某个时间段是否已经完成。
我突出了问题(图片)。
HTML标记
#option value: 00:00 till 23:45 with 15 minutes gaps
<select name="start_time[]"></select>
<select name="finish_time[]"></select>
PHP:
if ( is_array($_POST['start_time']) ) {
$json_combine = array_combine($_POST['start_time'], $_POST['finish_time']);
foreach ($json_combine as $jcs => $jcf ) {
if ( ($jcs > $jcf) OR ($jcs == $jcf) ) {
echo 'Start time must before finish time.';
break;
}
}
}
答案 0 :(得分:1)
也许您可以使用DateTime进行比较:
if ( is_array($_POST['start_time']) ) {
$json_combine = array_combine($_POST['start_time'], $_POST['finish_time']);
foreach ($json_combine as $jcs => $jcf ) {
$d1=new DateTime($jcs);
$d2=new DateTime($jcf);
if ($d1 >= $d2) {
echo 'Start time must before finish time.';
break;
}
}
}
答案 1 :(得分:0)
如果我正确理解它,可以试试这个:
foreach ($_POST['start_time'] as $index => $key) {
$start_time = strtotime($_POST['start_time'][$index+1]);
$finish_time = strtotime($_POST['finish_time'][$index]);
if ( ($start_time > $finish_time) OR ($start_time == $finish_time) ) {
echo 'Start time must before finish time.';
break;
}
}