筛选事件列表 Google 日历 PHP

Filtering Event List Google Calendar PHP

我正在尝试使用以下代码段从 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 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.';
}
?>

日期选择工作正常,显示了所选日期,但我收到以下 php 错误:

[29-May-2015 17:34:05 Europe/Berlin] PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/calendar/v3/calendars/bkrni7gfaiumlahibu0mnifjvk%40group.calendar.google.com/events?maxResults=10&orderBy=startTime&singleEvents=true&timeMin=2015-05-31CEST00%3A00: (400) Bad Request' in /html/calendar/api/google-api-php-client/src/Google/Http/REST.php:110
Stack trace:
#0 /html/calendar/api/google-api-php-client/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client))
#1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request))
#2 /html/calendar/api/google-api-php-client/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array)
#3 /html/calendar/api/google-api-php-client/src/Google/Http/REST.php(46): Google_Task_Runner->run()
#4 /html/calendar/api/google-api-php-client/src/Google/Client.php(590): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#5 /html/calendar/ap in /html/calendar/api/google-api-php-client/src/Google/Http/REST.php on line 110

为什么会失败,因为根据 this API 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);

我得到了想要的结果。