Google Calendar API, Free Slots

Hi Experts,

With the Google Calendar API, is there a way to get free 90min time slots that are available M-F 9:00-16:30 within a given week?

I was thinking the freebusy method, but I think that's for something else?

If there's no method, then is there a way to get all events for a given week, or do I need to keep getting events, until I see  I'm past the week?  

Using the events, in order for me to get the 90min time slots, I was thinking to loop through all events and see  if there's a 90min difference between the END of one event and the START of the next, and that the time is <= 15:00 (16:30 - 90 = 15:00).  

Am I over-complicating, or is this  the only way?
APD TorontoAsked:
Who is Participating?
Chris StanyonCommented:
You're going to have to retrieve the events using the FreeBusy query for a given week (set the TimeMin and TimeMax). This will pull in all events that you have between those dates, including the start and end times.

You will then need to decide on your own logic to determine what time is available. For example - if an event runs from 12:00-13:00 on a given day, how many slots does that leave available. Do slots always run from 9:00-10:30 // 10:30-12:00 // 12:00-13:30 etc.  Depending on your own business logic, it may be straight forward or it could be quite tricky.

Here's a quick demo on how to get the events for a given week, and build an array containing the start / end of each event (it assumes you've already got your Google_Service_Calendar setup as $service;

$freebusy = new Google_Service_Calendar_FreeBusyRequest();
$freebusy->setItems([array('id' => '')]);

$results = $service->freebusy->query($freebusy);

foreach ($results->calendars as $id => $data):
    foreach ($data->busy as $busy):
        $events[] = array(
            'start' => $busy->start,
            'end' => $busy->end

Open in new window

APD TorontoAuthor Commented:
Thank you Chris!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.