0

I want to check if any of the dates in the array will overlap the others. Only thing I want to know is if it true or false. It has to check if the start and stop time will overlap the time in the other rows.

This is an example of my arrays. In this case it should return false.

$testFalse = [
    'row_0' => [
        'startTime' => '2019-10-07 07:30:00',
        'stopTime' => '2019-10-07 09:00:00'   
    ],
    'row_1' => [
        'startTime' => '2019-10-07 07:30:00',
        'stopTime' => '2019-10-07 08:00:00'   
    ],
     'row_2' => [
        'startTime' => '2019-10-07 08:30:00',
        'stopTime' => '2019-10-07 10:00:00'   
    ],
];   

In this case it should return true.

$testTrue = [
    'row_0' => [
        'startTime' => '2019-10-07 07:30:00',
        'stopTime' => '2019-10-07 09:00:00'   
    ],
    'row_1' => [
        'startTime' => '2019-10-07 09:00:00',
        'stopTime' => '2019-10-07 10:00:00'   
    ],
];     

My current try is, but I wan't to combine this in a loop somehow:

//------------StartTime row-0--------------------
if (($array['row_0']['startTime'] >= $array['row_1']['startTime']) && ($array['row_0']['startTime'] <= $array['row_1']['stopTime'])){
    var_dump('Is between');
}else{
    var_dump('Not between');
}
if (($array['row_0']['startTime'] >= $array['row_2']['startTime']) && ($array['row_0']['startTime'] <= $array['row_2']['stopTime'])){
    var_dump('Is between');
}else{
    var_dump('Not between');
}
?>
4
  • 2
    Welcome! To ask On Topic question, please read Question Check list and the perfect question and how to create a Minimal, Complete and Verifiable Example and take the tour. We are very willing to help you fix your code, but we don't write code for you. Commented Oct 8, 2019 at 15:10
  • Just check if one of a next dates is between previous ones, f.e. like this Commented Oct 8, 2019 at 15:18
  • @freeek that is what I tried and got it working half. My problem is the multiple rows... Commented Oct 8, 2019 at 15:29
  • @Dave updated post... Commented Oct 8, 2019 at 15:41

1 Answer 1

2

You can just check if one of the dates is between:

$testTrue = [
    'row_0' => [
        'startTime' => '2019-10-07 07:30:00',
        'stopTime' => '2019-10-07 09:00:00'   
    ],
    'row_1' => [
        'startTime' => '2019-10-07 09:00:00',
        'stopTime' => '2019-10-07 10:00:00'   
    ],
];     

$testFalse = [
    'row_0' => [
        'startTime' => '2019-10-07 07:30:00',
        'stopTime' => '2019-10-07 09:00:00'   
    ],
    'row_1' => [
        'startTime' => '2019-10-07 07:30:00',
        'stopTime' => '2019-10-07 08:00:00'   
    ],
     'row_2' => [
        'startTime' => '2019-10-07 08:30:00',
        'stopTime' => '2019-10-07 10:00:00'   
    ],
];   

function checkIntersections(array $arr): bool {
    $count = count($arr);
    for($i=0;$i < $count;$i++) {
        $startDate = new DateTime($arr['row_'.$i]['startTime']);
        $stopDate = new DateTime($arr['row_'.$i]['stopTime']);

        for($j=0;$j < $count;$j++) {
            if ($j === $i) continue;
            $startDateCompare = new DateTime($arr['row_'.$j]['startTime']);
            $stopDateCompare = new DateTime($arr['row_'.$j]['stopTime']);

            if (
                ($startDate > $startDateCompare && $startDate < $stopDateCompare)
                || ($stopDate > $startDateCompare && $stopDate < $stopDateCompare)
            ) {
                return false;    
            }
        }

        return true;

    }
}

var_dump(checkIntersections($testTrue));
var_dump(checkIntersections($testFalse));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.