lethalMango
lethalMango

Reputation: 4491

Identify Clash from Day and Time Array

I have checked other SO questions but can't seem to find anything related to this - point me in the right direction if I missed something.

I currently have the following array containing a day and time (requested meeting time):

Array (
    [0] =>
        day => 'Thursday',
        start_time => '14:00',
        end_time => '15:00'
    [1] =>
        day => 'Thursday',
        start_time => '16:30',
        end_time => '17:30'
)

The user is required to attend both of these times. What I am attempting to do is compare against another array containing a users current appointments and detect if there is a clash / time overlap issue

Array (
    [0] =>
        day => 'Monday',
        start_time => '09:00',
        end_time => '13:00'
    [1] =>
        day => 'Wednesday',
        start_time => '10:45',
        end_time => '11:15'
    [2] =>
        day => 'Thursday',
        start_time => '16:45',
        end_time => '17:45'
)

Using the above example I want to identify there is a clash with the second timeslot [1] => of the first array (proposed meetings) which clashes with the third timeslot [2] => of the second array (current meetings).

I have seen examples with timestamps and full dates, but not just days and start / end times.

Upvotes: 4

Views: 557

Answers (3)

Baba
Baba

Reputation: 95111

You can do it this way

$slot1 = Array (
        0 => array (
                'day' => 'Thursday',
                'start_time' => '14:00',
                'end_time' => '15:00' 
        ),
        1 => array (
                'day' => 'Thursday',
                'start_time' => '16:30',
                'end_time' => '17:30' 
        ) 
);

$slot2 = Array (
        0 => array (
                'day' => 'Monday',
                'start_time' => '09:00',
                'end_time' => '13:00' 
        ),
        1 => array (
                'day' => 'Wednesday',
                'start_time' => '10:45',
                'end_time' => '11:15' 
        ),
        2 => array (
                'day' => 'Thursday',
                'start_time' => '16:45',
                'end_time' => '17:45' 
        ) 
);

echo "<pre>";
$format = "l H:s";
foreach($slot1 as $value)
{
    $slot1Start = DateTime::createFromFormat ($format , $value ['day'] . " " . $value['start_time'] );
    $slot1End = DateTime::createFromFormat ( $format , $value ['day'] . " " . $value['end_time'] );

    foreach($slot2 as $value2)
    {
        $slot2Start = DateTime::createFromFormat ( $format , $value2 ['day'] . " " .$value2['start_time'] );
        $slot2End = DateTime::createFromFormat ( $format , $value2 ['day'] . " " .$value2 ['end_time'] );


        if ($slot1Start <= $slot2End && $slot1End >= $slot2Start) {
            echo sprintf ( "Even Overlap on %s %s-%s AND %s %s-%s", $value ['day'], $value ['start_time'], $value ['end_time'],
                    $value2 ['day'], $value2 ['start_time'], $value2 ['end_time'] ), PHP_EOL;
        }

    }
}

Output

Even Overlap on Thursday 16:30-17:30 AND Thursday 16:45-17:45

Upvotes: 2

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

If they are all in a database, a query for conflicts could look like:

SELECT `plan`.*
FROM `plan`
    JOIN `current` ON `plan`.`day`=`current`.`day`
WHERE
    `plan`.`start_time` < `current`.`end_time`
    AND `plan`.`end_time` > `current`.`start_time`

where current is the table with the current schedule and plan is the table with the user's schedule. I haven't tested this just because I don't have an example, but this should return rows where the day is the same and plan times overlap current times.

edit: You might also want to do stuff like mark current as tentative if they are not required. you can put that into the query where clause.

Also the > and < mean that the start and end minute are not inclusive. so if one ends at the same time as another begins, this will not flag them as a conflict. If you want the start/end minute to be inclusive, change them to >= and <=.

Upvotes: 2

DaneSoul
DaneSoul

Reputation: 4511

There are your a bit modified arrays code

   $current_sheduler = Array (
        [0] =>
            day => 'Thursday',
            start_time => '14:00',
            end_time => '15:00'
        [1] =>
            day => 'Thursday',
            start_time => '16:30',
            end_time => '17:30'
    )

    $user_plans = Array (
        [0] =>
            plan_name => 'event1',
            day => 'Monday',
            start_time => '09:00',
            end_time => '13:00'
        [1] =>
            plan_name => 'event2',
            day => 'Wednesday',
            start_time => '10:45',
            end_time => '11:15'
        [2] =>
            plan_name => 'event3',
            day => 'Thursday',
            start_time => '16:45',
            end_time => '17:45'
    )

Here is way to process your arrays and catches if event planned by user matches time from your first array

foreach($user_plans as $plan){
  foreach($current_sheduler as current){
     if($plan['day']==$current['day']){
       if(
         ($plan['start_time']>=$current['start_time']) &&
         ($plan['end_time']<=$current['end_time'])
       ){
          echo "Event {$plan['plan_name']} is working now!";
       }
     }
  }
}

This is general logic, for situation, when match means full period match only.

Another notice: befor compare times, you need to recount them into counted minutes format.

Upvotes: 0

Related Questions