0

I need to check if two time ranges overlap. For example,

time1: 13:20 - 13:40
time2: 14:00 - 14:30
time3: 13:30 - 13:50

time1 and time3 overlap. How do I check this?

time1 and time2 have a gap of 20 minutes, but if the gap is less than 30 minutes it also should be considered overlapping.

Anyone can help me with this, thanks very much.

4

1 回答 1

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;
      }
   }
}
于 2012-05-25T03:48:05.097 回答