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 TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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->setTimeMin('2017-11-27T09:00:00Z');
$freebusy->setTimeMax('2017-12-03T16:30:00Z');
$freebusy->setItems([array('id' => 'yourAddress@gmail.com')]);

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

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor Commented:
Thank you Chris!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Google

From novice to tech pro — start learning today.