2012-10-27 20 views
0

我有一个php网站,在这里我需要创建一个页面来输入一个学生的考试细节。一门考试中有5门科目。每个科目有考试日期,开始时间和结束时间重复检查日期和时间使用php

即:

Date: 02-nov-2012 
Start time: 2.30 pm 
End time: 4.30 pm 

我必须检查考试日期时间的重复....也就是说,任何其他没有类似日期时间的科目也是如果一个科目有

Exam Date: 02-nov-2012 
Start time: 3.00.pm 
End date: 5.00 pm 

必须避免。这是无效的 另一个主题具有

Exam Date: 02-nov-2012 
Start time: 9.00.am 
End date: 2.00 pm 

这是一个有效的

我想很明显...我不知道在细节进一步解释.... 我不知道这个验证in php

+0

如果您想在PHP中进行比较,为什么不使用DateTime对象并比较两个对象。 –

回答

0
$errors = array(); 

for ($i = 0; $i < count($exams); $i++) { 
    for ($j = $i+1; $j < count($exams); $j++) { 
    if (($exams[$j]['start_time'] >= $exams[$i]['start_time'] && ($exams[$j]['start_time'] <= $exams[$i]['end_time']) || 
     ($exams[$j]['end_time'] >= $exams[$i]['start_time'] && ($exams[$j]['end_time'] <= $exams[$i]['end_time'])) { 
     $errors[] = "<li>Exams $i and $j overlap.</li>"; 
    } 
    } 
} 

if ($errors) { 
    echo "There were conflicts:<br><ul>\n ", join("\n ", $errors), "\n</ul>\n"; 
} else { 
    echo "Congratulations, no conflicts!\n"; 
}