首页 > 解决方案 > PHP 中的 Google-Calendar-Api 错误,“开始时间和结束时间必须要么都是日期,要么都是日期时间”

问题描述

我正在尝试更新谷歌日历中的事件,我正在使用谷歌日历 api,但是当我尝试设置事件的开始日期时间和结束日期时间时,我收到此错误:

 "code": 400, "message": "Start and end times must either both be date or both be dateTime." .

我实际上尝试将我的日期格式化为 ISO8601 和 RF3339,但我会得到不同类型的错误,其中日期格式无效

这是我的代码

$service = new Google_Service_Calendar($client);
  $event = $service->events->get($calendarID, $eventId);

$string_start_date ="12-07-2020 07:13:00"
  $start_date = new DateTime($string_start_date);
    $google_start_time = new Google_Service_calendar_eventDateTime();
      $google_start_time->setDateTime($start_date);

$string_end_date = "13-07-2020 12:00:00";
  $end_date = new DateTime($string_end_date);
    $google_end_time = new Google_Service_calendar_eventDateTime();
      $google_end_time->setDateTime($end_date);

$event->setDescription("description");
$event->setEnd($google_end_time);
$event->setStart($google_start_time);
$event->setSummary("summary");

$update = $service->events->update($calendarID, $eventId, $event);

这是我的两个 google_service_calendar_eventDateTime 对象的内容:

1:object(Google_Service_Calendar_EventDateTime)#31 (6) { ["date"]=> NULL ["dateTime"]=> object(DateTime)#52 (3) { ["date"]=> string(26) "2020-07-12 07:13:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } ["timeZone"]=> NULL ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } }

2:object(Google_Service_Calendar_EventDateTime)#58 (6) { ["date"]=> NULL ["dateTime"]=> object(DateTime)#73 (3) { ["date"]=> string(26) "2020-07-13 12:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } ["timeZone"]=> NULL ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } }

标签: phpdategoogle-apigoogle-calendar-api

解决方案


感谢@Nico Haase,我发现了一个类似的问题

这是我找到答案的链接

这是更新的代码:

$end_date = new DateTime($string_end_date);
   $google_end_time = new Google_Service_calendar_eventDateTime();
     $google_end_time->setDateTime($end_date->format(\DateTime::RFC3339));

推荐阅读