过滤事件列表Google日历PHP
我正在尝试使用以下代码段从google日历中获取位于特定用户给定日期之间的事件:过滤事件列表Google日历PHP,php,calendar,google-calendar-api,google-api-php-client,Php,Calendar,Google Calendar Api,Google Api Php Client,我正在尝试使用以下代码段从google日历中获取位于特定用户给定日期之间的事件: form name="dates" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Start: <input type="date" name="start"> End: <input type="date" name="end"> <br /> <input typ
form name="dates" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Start: <input type="date" name="start">
End: <input type="date" name="end">
<br />
<input type="submit" name="submit" value="Anzeigen">
</form>
<?php
if(isset($_POST['submit']))
{
echo "Dates were chosen: Start " . date($_POST['start']) . ' and End ' . date($_POST['end']);
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar($client);
// Print the next 10 events on the user's calendar.
$calendarId = 'bkrni7gfaiumlahibu0mnifjvk@group.calendar.google.com';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => date($_POST['start']),
);
$results = $service->events->listEvents($calendarId, $optParams);
if (count($results->getItems()) == 0) {
print "No upcoming events found.\n";
} else {
print "Upcoming events:\n";
foreach ($results->getItems() as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
printf("%s (%s)\n", $event->getSummary(), $start);
}
}
} else {
echo 'Bitte ein Start- und Enddatum auswählen.';
}
?>
为什么它会失败,因为根据timeMin应该是dateTime。多亏了luc的评论,我发现了这个问题。现在,使用
$timeMin = date($_POST['start']) . "T00:00:00Z";
$timeMax = date($_POST['end']) . "T00:00:00Z";
在对API的调用中
// Print appointments between given start and end date
$calendarId = 'bkrni7gfaiumlahibu0mnifjvk@group.calendar.google.com';
$optParams = array(
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => $timeMin,
'timeMax' => $timeMax,
);
$results = $service->events->listEvents($calendarId, $optParams);
我得到了期望的结果。您的时间分钟似乎有误:2015-05-31CEST00%3A00它应该根据RFC 3339进行格式化。比如2015-05-29T00:00:00Z非常感谢你。你的评论真的帮我弄明白了(见下面的答案)。
// Print appointments between given start and end date
$calendarId = 'bkrni7gfaiumlahibu0mnifjvk@group.calendar.google.com';
$optParams = array(
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => $timeMin,
'timeMax' => $timeMax,
);
$results = $service->events->listEvents($calendarId, $optParams);