【发布时间】:2018-02-20 12:50:53
【问题描述】:
我正在尝试使用 PHP 和 Google Calendar API 检索特定日期的事件,但在获取正确日期的事件时遇到问题。例如,我可以在日历中看到该事件,但没有使用 API 检索它,或者日历中没有任何内容并且它正在获取前一天的事件。
在研究和试验之后,我认为这可能是时区的问题,但我看不出我的代码有什么问题。值得一提的是,日历中的所有事件都是全天事件。
非常感谢您能提供的任何帮助。 (我是新手编码员,这是我的第一个问题,所以如果我错过了什么,请告诉我!)
$eventdate = "26-08-2018";
require_once "google-api-php-client-2.2.1/vendor/autoload.php";
putenv("GOOGLE_APPLICATION_CREDENTIALS=MY_CREDENTIALS");
$scope = 'https://www.googleapis.com/auth/calendar';
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes($scope);
$service = new Google_Service_Calendar($client);
$googleeventdate = date("Y-m-d", strtotime($eventdate));
$optParams = array(
"timeZone" => "Europe/London",
"timeMin" => $googleeventdate . "T00:00:00Z",
"timeMax" => $googleeventdate . "T23:59:59Z"
);
$events = $service->events->listEvents($googlecal, $optParams);
foreach ($events->getItems() as $event) {
echo "Events in Calendar: " . $event->summary;
}
【问题讨论】:
-
我建议你使用谷歌日历V3。然后使用
get方法检索事件。基于此thread,Google 于 2014 年 11 月 17 日停止了对 v2 的支持。请使用 Google Calendar API 检查此PHP Quickstart。 -
最后我采取了稍微不同的方法。我检索了较长时间段内的所有事件,并使用 php 检查我的特定日期是否在该时间段内。知道 Google 同时使用 getDate 和 getDateTime 也很有用,总结为link