1

Hope anybody can help me here. I have 2 Arrays with given start and end times. What i need to do is to check if the time range from Array 1 is in the range of Array 2 and if it is in the range it have to be removed from Array 2.

Array 1:

Array ( 
      [0] => Array ( [from] => 10:00:00 [to] => 10:30:00 ) 
      [1] => Array ( [from] => 11:00:00 [to] => 11:45:00 )
      [2] => Array ( [from] => 14:00:00 [to] => 15:00:00 ) 
      [3] => Array ( [from] => 16:30:00 [to] => 17:00:00 ) 
)

Array 2:

Array ( 
      [0] => Array ( [from] => 09:30 [to] => 10:00 ) 
      [1] => Array ( [from] => 10:15 [to] => 10:45 ) 
      [2] => Array ( [from] => 11:00 [to] => 11:30 ) 
      [3] => Array ( [from] => 11:45 [to] => 12:15 ) 
      [4] => Array ( [from] => 12:30 [to] => 13:00 ) 
      [5] => Array ( [from] => 13:15 [to] => 13:45 ) 
      [6] => Array ( [from] => 14:00 [to] => 14:30 ) 
      [7] => Array ( [from] => 14:45 [to] => 15:15 ) 
      [8] => Array ( [from] => 15:30 [to] => 16:00 ) 
      [9] => Array ( [from] => 16:15 [to] => 16:45 ) 
      [10] => Array ( [from] => 17:00 [to] => 17:30 ) 
      [11] => Array ( [from] => 17:45 [to] => 18:15 ) 
      [12] => Array ( [from] => 18:30 [to] => 19:00 ) 
      [13] => Array ( [from] => 19:15 [to] => 19:45 ) 
) 

Hope there is somebody out there who can help me with this. Thanks in advance for your help!

0

1 Answer 1

1

PS: Edited as the OP has provided more details.

This should be a comment, but: o "reputation!" you made this an answer!!

Your question was incomplete and ambiguous! (more information added Afterwards)

Just check every available time slot against the reserved ones!, and if the available time slot is still "available", then add it to a temporary array and finally update the available time slots' array:

<?php
$array1 = Array (
0 => Array ( 'from' => '10:00:00', 'to' => '10:30:00' ),
1 => Array ( 'from' => '11:00:00', 'to' => '11:45:00' ),
2 => Array ( 'from' => '14:00:00', 'to' => '15:00:00' ),
3 => Array ( 'from' => '16:30:00', 'to' => '17:00:00' ) );

$array2 = Array(
0 => Array ( 'from' => '09:30', 'to' => '10:00' ),
1 => Array ( 'from' => '10:15', 'to' => '10:45' ),
2 => Array ( 'from' => '11:00', 'to' => '11:30' ),
3 => Array ( 'from' => '11:45', 'to' => '12:15' ),
4 => Array ( 'from' => '12:30', 'to' => '13:00' ),
5 => Array ( 'from' => '13:15', 'to' => '13:45' ),
6 => Array ( 'from' => '14:00', 'to' => '14:30' ),
7 => Array ( 'from' => '14:45', 'to' => '15:15' ),
8 => Array ( 'from' => '15:30', 'to' => '16:00' ),
9 => Array ( 'from' => '16:15', 'to' => '16:45' ),
10 => Array ( 'from' => '17:00', 'to' => '17:30' ),
11 => Array ( 'from' => '17:45', 'to' => '18:15' ),
12 => Array ( 'from' => '18:30', 'to' => '19:00' ),
13 => Array ( 'from' => '19:15', 'to' => '19:45' ) );

$a2 = array();
foreach( $array2 as $timeRange2 )
{
    $a2f = strtotime($timeRange2['from']);
    $a2t = strtotime($timeRange2['to']);
    $bInArray2 = false;
    foreach ( $array1 as $timeRange1 )
    {
        $a1f = strtotime($timeRange1['from']);
        $a1t = strtotime($timeRange1['to']);

        if(($a1f >= $a2f && $a1f <= $a2t) || ($a1t >= $a2f && $a1t <= $a2t))
        {
            $bInArray2 = true;
            break;
        }
    }
    if(!$bInArray2)
        $a2[] = $timeRange2;
}
$array2 = $a2;
echo var_dump($array2);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help, but the result is not correct. In Array1 are reserved time slots and in Array2 are the available time slots. As you can see there are 15 Minutes between each time slot. Sample: Reserved from 10:00 - 10:30 Available from 09:30 - 10:00 and 10:15 - 10:45 So the time slots from 09:30 - 10:45 should not be in the result, cause it is in the range of the reserved time slot from 10:00 - 10:30. Hope you can now understand how i mean. Thanks in advance.

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.