我需要检查两个时间范围是否重叠。 例如,
time1:13:20-13:40
时间2:14:00-14:30
时间3:13:30-13:50
time1和time3重叠。我该如何检查?
time1和time2的差距为20分钟,但如果差距小于30分钟,也应视为重叠。
任何人都可以帮助我,非常感谢。
答案 0 :(得分:0)
此?
$_30mins = 60*30; // 60 secs * 30 minutes
// make the times into seconds
$times = array();
$times[] = array("from"=>mktime(13,20,0,1,1,1970),"to"=>mktime(13,40,0,1,1,1970));
$times[] = array("from"=>mktime(14,00,0,1,1,1970),"to"=>mktime(14,30,0,1,1,1970));
$times[] = array("from"=>mktime(13,30,0,1,1,1970),"to"=>mktime(13,50,0,1,1,1970));
foreach($times as $key=>$val) {
foreach($times as $key2=>$val2) {
if ($key2!=$key && (($val["from"]>=$val2["from"] && $val["from"]<=$val2["to"]) ||
(abs($val["to"]-$val2["from"])<$_30mins))) {
echo "time $key overlaps with $key2<br>";
break;
}
}
}