Test if a range intersects another range of numbers

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

I have 2 range of numbers:

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

相关标签:
6条回答
  • 2021-01-07 03:57

    After some time I have found an efficient simple solution that seems to work (feel free to tell me if it doesn't).

    ($offerStartTime <= $startTime && $offerEndTime > $startTime) ||
    ($offerStartTime > $startTime && $offerStartTime < $endTime)
    

    I will also be looking at answers that are better than mine, so please still send me your solution.

    0 讨论(0)
  • 2021-01-07 04:05

    If you are just checking whether any part of the offer overlaps any part of the range, it's simple.

    if ($offerStartTime < $endTime && $offerEndTime > $startTime)  {
        echo 'The ranges overlap';
    }
    

    Here's a picture representing all the possibilities of overlap and non-overlap to visualize why this works.

    Based on your inputs and expected false outputs, I used < and >. If you wanted to also include ranges that intersected at a point, you would need to use <= and >= instead.

    0 讨论(0)
  • 2021-01-07 04:10

    Hope this will help you out, I have tried this with all your inputs and it is working fine..

    <?php
    
    ini_set('display_errors', 1);
    
    $startTime=10;
    $endTime=20;
    
    $startOffset=100;
    $endOffset=110;
    
    $range=range($startTime,$endTime);//getting range of time
    
    $offsetRange=range($startOffset,$endOffset);//getting range of offset
    
    
    $set=array_intersect($range, $offsetRange);//getting the intersection
    
    if(is_array($set) && count($set)>0);
    {
        if(count($set)>1)
        {
            echo "Matched";
        }
        //added this to prevent this case offerStartTime: 1, offerEndTime: 10, 
        //ranges intersection is the endTime
        elseif(count($set)==1)
        {
            if($set[0]!=$startTime)
            {
                echo "Matched";
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-07 04:12

    You can use range and array_intersect, i.e.:

    function time_intersect($startTime, $endTime, $offerStartTime, $offerEndTime)
    {
        $start_to_end = range($startTime, $endTime, 1);
        $offer_start_end = range($offerStartTime, $offerEndTime, 1);
        if (!empty(array_intersect($start_to_end, $offer_start_end)))
        {
          # time overlaps
          return true;
        }
    }
    

    Explanation:

    With range we create 2 arrays containing numbers based on the 4 variables (start, end), then we use array_intersect to check if the 2 arrays have numbers in common, if the output is not empty, we know the numbers (time) overlap.

    0 讨论(0)
  • 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.

    <?php
    
    $innerRange = ['start' => 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;
    }
    
    ?>
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题