Avatar of APD Toronto
APD Toronto
Flag for Canada asked on

Logic for Time Slot Availability

Hi Experts;

I need to figure out 90min free time slots, for each day in question, but I can't seem to get  it quite right.

So far I have the following 3 arrays, which show correct data below ($days, $events, $daily_slots):

$days - are the days in question:
=========START=========
days
Array
(
    [0] => 2017-12-04
    [1] => 2017-12-05
    [2] => 2017-12-06
    [3] => 2017-12-07
    [4] => 2017-12-08
)

=========END=========

Open in new window


$events - are events already in my calendar
=========START=========
events
Array
(
    [0] => Array
        (
            [name] => Meeting with Client
            [date] => 2017-12-04
            [start] => 09:00
            [end] => 12:00
        )

    [1] => Array
        (
            [name] => Working on Project
            [date] => 2017-12-05
            [start] => 10:30
            [end] => 12:30
        )

    [2] => Array
        (
            [name] => Teleconference with D
            [date] => 2017-12-05
            [start] => 15:30
            [end] => 16:30
        )

    [3] => Array
        (
            [name] => Meeting with Client
            [date] => 2017-12-06
            [start] => 10:00
            [end] => 13:00
        )

    [4] => Array
        (
            [name] => Financial Advisor
            [date] => 2017-12-06
            [start] => 14:30
            [end] => 17:00
        )

    [5] => Array
        (
            [name] => Workshop
            [date] => 2017-12-07
            [start] => 10:30
            [end] => 13:30
        )

    [6] => Array
        (
            [name] => Another Event
            [date] => 2017-12-07
            [start] => 15:30
            [end] => 16:30
        )

)

=========END=========

Open in new window


$daily_slots - these are all possible 90min time slot that any work days can have, between 9 and 4 in 30 minutes increments
=========START=========
slots
Array
(
    [0] => Array
        (
            [from] => 09:00
            [to] => 10:30
        )

    [1] => Array
        (
            [from] => 09:30
            [to] => 11:00
        )

    [2] => Array
        (
            [from] => 10:00
            [to] => 11:30
        )

    [3] => Array
        (
            [from] => 10:30
            [to] => 12:00
        )

    [4] => Array
        (
            [from] => 11:00
            [to] => 12:30
        )

    [5] => Array
        (
            [from] => 11:30
            [to] => 13:00
        )

    [6] => Array
        (
            [from] => 12:00
            [to] => 13:30
        )

    [7] => Array
        (
            [from] => 12:30
            [to] => 14:00
        )

    [8] => Array
        (
            [from] => 13:00
            [to] => 14:30
        )

    [9] => Array
        (
            [from] => 13:30
            [to] => 15:00
        )

    [10] => Array
        (
            [from] => 14:00
            [to] => 15:30
        )

    [11] => Array
        (
            [from] => 14:30
            [to] => 16:00
        )

)

=========END=========

Open in new window



As mentioned I believe that all 3 above are correct. That said, I need to get all free 90 minutes slots based on this.

I have tried the following
//Loop through each day and get available slots
        $available = array();
        $available_counter = -1;
        
        foreach($days as $day):
            
            $available_counter++;
            $available[$available_counter]['date'] = date('D. M. d', strtotime($day));
            $slots = array();
            
            foreach($daily_slots as $day_slot):
                
                foreach($events as $event):
                
                    if (strtotime($event['date']) == strtotime($day)){
                        
                        $slot_form = date($day . strtotime($day_slot['from']));
                        $slot_to = date($day . strtotime($day_slot['to']));
                        
                        
                        if (($day_slot['to'] <= $event['start']) ||
                           ($day . $day_slot['from'] >= $event['end'])){ //Day Slot is available
                    
  //                      if (strtotime($day . $day_slot['to']) <= (strtotime($event['start'])) ||
//                            (strtotime($day . $day_slot['from']) >= (strtotime($event['end'])))){ //Day Slot is available
                            
                                $slots[] = $day_slot;
                                echo 'available';
                        } else { echo 'not available'; }
                        
                    }
                
                endforeach; //event
                
            endforeach; //Daily Slots
            
            $available[$available_counter]['slots'] = $slots;
            
        endforeach; //Days
       

Open in new window


...But I am not gettign what I need, and as you can see I have tried a few things. My latest output of $available is
=========START=========
available
Array
(
    [0] => Array
        (
            [date] => Mon. Dec. 04
            [slots] => Array
                (
                    [0] => Array
                        (
                            [from] => 09:00
                            [to] => 10:30
                        )

                    [1] => Array
                        (
                            [from] => 09:30
                            [to] => 11:00
                        )

                    [2] => Array
                        (
                            [from] => 10:00
                            [to] => 11:30
                        )

                    [3] => Array
                        (
                            [from] => 10:30
                            [to] => 12:00
                        )

                    [4] => Array
                        (
                            [from] => 11:00
                            [to] => 12:30
                        )

                    [5] => Array
                        (
                            [from] => 11:30
                            [to] => 13:00
                        )

                    [6] => Array
                        (
                            [from] => 12:00
                            [to] => 13:30
                        )

                    [7] => Array
                        (
                            [from] => 12:30
                            [to] => 14:00
                        )

                    [8] => Array
                        (
                            [from] => 13:00
                            [to] => 14:30
                        )

                    [9] => Array
                        (
                            [from] => 13:30
                            [to] => 15:00
                        )

                    [10] => Array
                        (
                            [from] => 14:00
                            [to] => 15:30
                        )

                    [11] => Array
                        (
                            [from] => 14:30
                            [to] => 16:00
                        )

                )

        )

    [1] => Array
        (
            [date] => Tue. Dec. 05
            [slots] => Array
                (
                    [0] => Array
                        (
                            [from] => 09:00
                            [to] => 10:30
                        )

                    [1] => Array
                        (
                            [from] => 09:00
                            [to] => 10:30
                        )

                    [2] => Array
                        (
                            [from] => 09:30
                            [to] => 11:00
                        )

                    [3] => Array
                        (
                            [from] => 09:30
                            [to] => 11:00
                        )

                    [4] => Array
                        (
                            [from] => 10:00
                            [to] => 11:30
                        )

                    [5] => Array
                        (
                            [from] => 10:00
                            [to] => 11:30
                        )

                    [6] => Array
                        (
                            [from] => 10:30
                            [to] => 12:00
                        )

                    [7] => Array
                        (
                            [from] => 10:30
                            [to] => 12:00
                        )

                    [8] => Array
                        (
                            [from] => 11:00
                            [to] => 12:30
                        )

                    [9] => Array
                        (
                            [from] => 11:00
                            [to] => 12:30
                        )

                    [10] => Array
                        (
                            [from] => 11:30
                            [to] => 13:00
                        )

                    [11] => Array
                        (
                            [from] => 11:30
                            [to] => 13:00
                        )

                    [12] => Array
                        (
                            [from] => 12:00
                            [to] => 13:30
                        )

                    [13] => Array
                        (
                            [from] => 12:00
                            [to] => 13:30
                        )

                    [14] => Array
                        (
                            [from] => 12:30
                            [to] => 14:00
                        )

                    [15] => Array
                        (
                            [from] => 12:30
                            [to] => 14:00
                        )

                    [16] => Array
                        (
                            [from] => 13:00
                            [to] => 14:30
                        )

                    [17] => Array
                        (
                            [from] => 13:00
                            [to] => 14:30
                        )

                    [18] => Array
                        (
                            [from] => 13:30
                            [to] => 15:00
                        )

                    [19] => Array
                        (
                            [from] => 13:30
                            [to] => 15:00
                        )

                    [20] => Array
                        (
                            [from] => 14:00
                            [to] => 15:30
                        )

                    [21] => Array
                        (
                            [from] => 14:00
                            [to] => 15:30
                        )

                    [22] => Array
                        (
                            [from] => 14:30
                            [to] => 16:00
                        )

                    [23] => Array
                        (
                            [from] => 14:30
                            [to] => 16:00
                        )

                )

        )

    [2] => Array
        (
            [date] => Wed. Dec. 06
            [slots] => Array
                (
                    [0] => Array
                        (
                            [from] => 09:00
                            [to] => 10:30
                        )

                    [1] => Array
                        (
                            [from] => 09:00
                            [to] => 10:30
                        )

                    [2] => Array
                        (
                            [from] => 09:30
                            [to] => 11:00
                        )

                    [3] => Array
                        (
                            [from] => 09:30
                            [to] => 11:00
                        )

                    [4] => Array
                        (
                            [from] => 10:00
                            [to] => 11:30
                        )

                    [5] => Array
                        (
                            [from] => 10:00
                            [to] => 11:30
                        )

                    [6] => Array
                        (
                            [from] => 10:30
                            [to] => 12:00
                        )

                    [7] => Array
                        (
                            [from] => 10:30
                            [to] => 12:00
                        )

                    [8] => Array
                        (
                            [from] => 11:00
                            [to] => 12:30
                        )

                    [9] => Array
                        (
                            [from] => 11:00
                            [to] => 12:30
                        )

                    [10] => Array
                        (
                            [from] => 11:30
                            [to] => 13:00
                        )

                    [11] => Array
                        (
                            [from] => 11:30
                            [to] => 13:00
                        )

                    [12] => Array
                        (
                            [from] => 12:00
                            [to] => 13:30
                        )

                    [13] => Array
                        (
                            [from] => 12:00
                            [to] => 13:30
                        )

                    [14] => Array
                        (
                            [from] => 12:30
                            [to] => 14:00
                        )

                    [15] => Array
                        (
                            [from] => 12:30
                            [to] => 14:00
                        )

                    [16] => Array
                        (
                            [from] => 13:00
                            [to] => 14:30
                        )

                    [17] => Array
                        (
                            [from] => 13:00
                            [to] => 14:30
                        )

                    [18] => Array
                        (
                            [from] => 13:30
                            [to] => 15:00
                        )

                    [19] => Array
                        (
                            [from] => 13:30
                            [to] => 15:00
                        )

                    [20] => Array
                        (
                            [from] => 14:00
                            [to] => 15:30
                        )

                    [21] => Array
                        (
                            [from] => 14:00
                            [to] => 15:30
                        )

                    [22] => Array
                        (
                            [from] => 14:30
                            [to] => 16:00
                        )

                    [23] => Array
                        (
                            [from] => 14:30
                            [to] => 16:00
                        )

                )

        )

    [3] => Array
        (
            [date] => Thu. Dec. 07
            [slots] => Array
                (
                    [0] => Array
                        (
                            [from] => 09:00
                            [to] => 10:30
                        )

                    [1] => Array
                        (
                            [from] => 09:00
                            [to] => 10:30
                        )

                    [2] => Array
                        (
                            [from] => 09:30
                            [to] => 11:00
                        )

                    [3] => Array
                        (
                            [from] => 09:30
                            [to] => 11:00
                        )

                    [4] => Array
                        (
                            [from] => 10:00
                            [to] => 11:30
                        )

                    [5] => Array
                        (
                            [from] => 10:00
                            [to] => 11:30
                        )

                    [6] => Array
                        (
                            [from] => 10:30
                            [to] => 12:00
                        )

                    [7] => Array
                        (
                            [from] => 10:30
                            [to] => 12:00
                        )

                    [8] => Array
                        (
                            [from] => 11:00
                            [to] => 12:30
                        )

                    [9] => Array
                        (
                            [from] => 11:00
                            [to] => 12:30
                        )

                    [10] => Array
                        (
                            [from] => 11:30
                            [to] => 13:00
                        )

                    [11] => Array
                        (
                            [from] => 11:30
                            [to] => 13:00
                        )

                    [12] => Array
                        (
                            [from] => 12:00
                            [to] => 13:30
                        )

                    [13] => Array
                        (
                            [from] => 12:00
                            [to] => 13:30
                        )

                    [14] => Array
                        (
                            [from] => 12:30
                            [to] => 14:00
                        )

                    [15] => Array
                        (
                            [from] => 12:30
                            [to] => 14:00
                        )

                    [16] => Array
                        (
                            [from] => 13:00
                            [to] => 14:30
                        )

                    [17] => Array
                        (
                            [from] => 13:00
                            [to] => 14:30
                        )

                    [18] => Array
                        (
                            [from] => 13:30
                            [to] => 15:00
                        )

                    [19] => Array
                        (
                            [from] => 13:30
                            [to] => 15:00
                        )

                    [20] => Array
                        (
                            [from] => 14:00
                            [to] => 15:30
                        )

                    [21] => Array
                        (
                            [from] => 14:00
                            [to] => 15:30
                        )

                    [22] => Array
                        (
                            [from] => 14:30
                            [to] => 16:00
                        )

                    [23] => Array
                        (
                            [from] => 14:30
                            [to] => 16:00
                        )

                )

        )

    [4] => Array
        (
            [date] => Fri. Dec. 08
            [slots] => Array
                (
                )

        )

)

=========END=========

Open in new window


I had thought that the following should work: if slot ends before or at event start OR if slot starts after or at event end, then slot is available, but it is not happening.

Capture.JPG
PHP

Avatar of undefined
Last Comment
APD Toronto

8/22/2022 - Mon
SOLUTION
David Favor

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
APD Toronto

ASKER
Thank you!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes