How to retrieve events between timeMin and timeMax?

9,605 views
Skip to first unread message

Cherrot Luo

unread,
Dec 3, 2011, 6:34:10 PM12/3/11
to google-api...@googlegroups.com
Hi, all! 
I'm a newbie here and recently I'm a little confused using Google Calendar APIs.
I try to use this:
$eventsParam = array("timeMin" => "20111203T000000", "timeMax" => "20111204T000000");
$events = $service->events->listEvents('primary', $eventsParam);

But it returns error:
PHP Fatal error:  Uncaught exception 'apiServiceException' with message 'Error calling GET https://www.googleapis.com/calendar/v3/calendars/primary/events?timeMin=20111203T000000&timeMax=20111204T000000&alt=json&key=XXX: (400) Bad Request ....

Could someone tell me how to correctly retrieve events between two days?
Thanks in advance and best regards!
Message has been deleted

Cherrot Luo

unread,
Dec 3, 2011, 7:54:01 PM12/3/11
to google-api...@googlegroups.com
Now I could retrieve events between two datetime by setting timeMin and timeMax to proper datetime format.
But the new problem is, In case of $event being an recurring event, when I call $event->getStart(), I got the event's original startTime, not the actual startTime in the day. My question is how to get the actual startTime of this event? 
Thanks!

Cherrot Luo

unread,
Dec 3, 2011, 8:03:39 PM12/3/11
to google-api...@googlegroups.com
Oh,  I know... Just set singleEvents to be true, then I could get the single event of the recurring events.

Chirag Shah

unread,
Dec 3, 2011, 9:14:29 PM12/3/11
to google-api...@googlegroups.com
Hey Cherrot,

I'm glad that it is working for you now! Please let us know if you see any other issues.

meL

unread,
Jan 17, 2012, 12:10:39 PM1/17/12
to google-api-php-client
Hi!
I'm stuck with this kind of problem. How did you resolve this? What
you mean with proper dateTime format?

my code right now is this:

$optEvent = array('timeMax' => '2012-01-18T00:00:00');
$events = $calendarService->events->listEvents($idCalendar,
$optEvent);

Thanks in advance,
Best Regards.
--
SP

On 4 Dic 2011, 00:34, Cherrot Luo <ad...@cherrot.com> wrote:
> Hi, all!
> I'm a newbie here and recently I'm a little confused using Google Calendar
> APIs.
> I try to use this:
> $eventsParam = array("timeMin" => "20111203T000000", "timeMax" =>
> "20111204T000000");
> $events = $service->events->listEvents('primary', $eventsParam);
>
> But it returns error:
> PHP Fatal error:  Uncaught exception 'apiServiceException' with message
> 'Error calling GEThttps://www.googleapis.com/calendar/v3/calendars/primary/events?timeM...

Cherrot Luo

unread,
Jan 18, 2012, 6:36:18 AM1/18/12
to google-api...@googlegroups.com
What I mean proper format is the return value of php function date(DateTime::ATOM), such as " 2012-01-18T19:27:05+0000"
Here is my code to retrieve a list of events of this week.

$optParam = array("orderBy"=>"startTime",
                                 "singleEvents"=>true,
                                 "timeMin"=>date(DateTime::ATOM) ,
                                 "timeMax"=>date(DateTime::ATOM, time()+(7 * 24 * 60 * 60)));
            $events = self::$service->events->listEvents('primary', $optParam);

Hope this helps :)

meL

unread,
Jan 18, 2012, 7:01:58 AM1/18/12
to google-api-php-client
Thanks a lot, that worked pretty well :)

gkarlis

unread,
May 13, 2012, 3:35:21 PM5/13/12
to google-api...@googlegroups.com
Hello, retrieving events works nicely the way you described, but I faced problem (Warning: Invalid argument supplied for foreach()...) when passing date from some function, that is returning exacly the same date( DateTime::ATOM ) value (like, 2012-05-13T19:24:57+00:00), when I print it outside array. What could cause this problem?

Chirag Shah

unread,
May 15, 2012, 7:29:06 PM5/15/12
to google-api...@googlegroups.com
Hi Gkarlis,

I'm curious to know where that warning is coming from. Is it coming from files in the Google APIs PHP client, or from somewhere else?
Typically, you can guard against this type of issue by checking is_array($arr) before traversing $arr with foreach.

if (is_array($arr) {
  foreach ($arr as $item) {
  // Use $item
  }
}

Thank you,
Chirag Shah

gkarlis

unread,
May 23, 2012, 12:32:20 PM5/23/12
to google-api...@googlegroups.com
I managed to find problem, the function I wrote was wrong, I was assigning not comparing values inside if branch, but my eye did not catch this for long time.
Reply all
Reply to author
Forward
0 new messages