【问题标题】:Problems retrieving date using Google Calendar API使用 Google Calendar API 检索日期时出现问题
【发布时间】: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
  • 对不起,我可能实际上正在使用 V3。我正在使用来自link 的最新版本和来自link 的示例代码。 get 方法似乎允许我使用事件 id 获取特定事件,但不能使用特定日期,这就是我使用 list 方法的原因。
  • 最后我采取了稍微不同的方法。我检索了较长时间段内的所有事件,并使用 php 检查我的特定日期是否在该时间段内。知道 Google 同时使用 getDate 和 getDateTime 也很有用,总结为link

标签: php google-calendar-api


【解决方案1】:

这是设置时间最小值和时间最大值的正确方法

// $eventdate = "26-08-2018";
$timeMin = date("c", strtotime( $eventdate) ); // gives 2018-08-26T00:00:00-04:00
$timeMax = date("c", strtotime( $eventdate.' +1 days') ); // gives 2018-08-27T00:00:00-04:00

你可以测试一下here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多