PaulH
PaulH

Reputation: 7863

How to get current events from google calendar API?

I'm trying to use the Google Calendar API method gapi.client.calendar.events.list to retrieve events on the calendar now.

I can successfully filter on start date and end date. But, filtering on things that start today doesn't help me if an event started yesterday and is still on-going.

    var request = gapi.client.calendar.events.list({
      'calendarId': 'my calendar id',
      // shows things that have not yet started
      'timeMin': (new Date()).toISOString(),
      'showDeleted': false,
      'singleEvents': true,
      'orderBy': 'startTime'
    });

How do I find the list of things on a calendar that are currently going on?

I'd (obviously) like to avoid pulling a week's worth of events and parsing through it.

Upvotes: 3

Views: 5730

Answers (2)

Abhishek Kr
Abhishek Kr

Reputation: 1

So I came to this thread in search of the same answer but to no avail, So here's what you can do :

    $data = self::$service->events->listEvents(self::$calendarId, $optParams);
    $curr_time = time();
    $active_meetings = array();

    if (count($data->getItems()) > 0) {  
        foreach ($data->getItems() as $event) {
            $start = date("U", strtotime($event->start->dateTime));
            $end   = date("U", strtotime($event->end->dateTime));
            if(($start > $curr_time) && ($end < $curr_time))
            {
                $active_meetings[$event->id] = $event->summary;
            }   
        }
    }
    return $active_meetings;

Here just taking events back to 1 hour before to events till 1 hour after and then comparing their start time and end time to current time.

This will list current ongoing events. Thanks

Upvotes: 0

Android Enthusiast
Android Enthusiast

Reputation: 4950

There isn't a way of querying for the current event directly, but you can certainly give it a limited min and max time, so you'll only have a small number of events to search through. If your events are regular and of predictable duration, you may be able to make it just give you one.

events = client.events().list(calendarId='primary',
                              timeMin='2011-12-22T09:00:00Z',
                              timeMax='2011-12-22T22:00:00Z').execute()

For more information, please read the Official Documentation of Google Calendar API: https://developers.google.com/google-apps/calendar/?csw=1

Upvotes: 1

Related Questions