Test if a range intersects another range of numbers

前端 未结 6 1462
小鲜肉
小鲜肉 2021-01-07 03:41

I have 2 range of numbers:

  • $startTime to $endTime
  • $offerStartTime to $offerEndTime

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 04:20

    //$startTime to $endTime
    //$offerStartTime to $offerEndTime
    //you can compare php times by using normal comparators so this is just a logic problem. here's the solution.
    
    
    //ok let's start by making sure that neither offered time is within the range because if it is we KNOW it's already good so
    
    if(($offerStartTime < $endTime && offerStartTime > $startTime) || ($offerEndTime < $endTime && offerEndTime > $startTime)){
          return true;
     }
     //so it's not easily already within the range so we have to test if the lower one is under the starting one but the other is above. ie.
    elseif(($offerStartTime < $startTime) && ($offerEndTime > $startTime)){
         return true; 
    }
    //so the only other acceptable possibility is that the offered start time is lower than the endtime and the offered end time is higher ie
    elseif(($offerStartTime < $endTime) && ($offerEndTime > $endTime)){
          return true;
    }
    //so we've exhausted all other valid possibilities it must be false
    else{
          return false;
    }
    

提交回复
热议问题