I've a script where I try to get all upcoming events from a Google Calendar.
Regarding to the API documentation, I have used "timeMin" for this, but it still gives me results in the past.
Have Google changed anything about this and if yes, what is the new way to do this?
My code is:
$calendarId = 'primary';
$optParams = array(
'maxResults' => 100,
'timeMin' => ''.date("Y-m-d",time()).'T00:00:00-00:00'
);
try{
$events = $service->events->listEvents('primary',$optParams);
}catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
My Answer:
the `timeMin` and `timeMax` parameters to specify the time range for the events I want to retrieve. However, when I make the request, I still get events that are outside of this time range.
Here is an example of the code I am using:
php
$calendarId = 'your_calendar_id_here';
$apiKey = 'your_api_key_here';
$timeMin = date('c');
$timeMax = date('c', strtotime('+1 week'));
$url = "https://www.googleapis.com/calendar/v3/calendars/{$calendarId}/events?key={$apiKey}&timeMin={$timeMin}&timeMax={$timeMax}";
$response = file_get_contents($url);
$data = json_decode($response, true);
foreach ($data['items'] as $event) {
echo $event['summary'] . "\n";
}
I have double-checked the `timeMin` and `timeMax` values and they seem to be correct. Is there something I am missing or doing wrong in my code that is causing me to get events outside of the specified time range?
Rate this post
5 of 5 based on 2818 votesComments