Test if a range intersects another range of numbers

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

I have 2 range of numbers:

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

6条回答
  •  天命终不由人
    2021-01-07 04:18

    You need to test if the start of your smaller range is greater or equal to your larger range's start AND if your end of your smaller range is less than or equal to the end of your larger range:

    In this case 10-20 falls within 5-25, so returns true. If you don't want to include the endpoints of the range simply change <= and >= to < and > respectively.

     10, 'end' => 20];
    $outerRange = ['start' => 5, 'end' => 25];
    
    echo isInRange($innerRange,$outerRange);
    
    function isInRange($innerRange,$outerRange) {
        if ($innerRange['start'] >= $outerRange['start'] && $innerRange['end'] <= $outerRange['end'] ) {
             return true;   
        }
        return false;
    }
    
    ?>
    

提交回复
热议问题