troubleshooting Question

Logic for Time Slot Availability

Avatar of APD Toronto
APD TorontoFlag for Canada asked on
PHP
3 Comments2 Solutions232 ViewsLast Modified:
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=========

$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=========

$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=========


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
       

...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=========

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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros