谷歌日历API日期时间

3

我正在尝试使用Google日历API,如果不使用我的$_POST日期时间,它可以完美地工作。但是,那时日期是硬编码的,就像谷歌一样,这不是我想要的。当我使用我的$_POST变量时,它会显示以下错误:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/%40group.calendar.google.com/events?key=: (400) **Start and end times must either both be date or both be dateTime**.'

我的dateTime代码:

  if ($client->getAccessToken()){
  $dt = new DateTime();
  echo $dt->format("Y-m-d\TH:i:s.000+01:00") . "\n";

  if (isset($_POST['addevent'])){
  echo "<hr><font size=+1>I have access to your calendar</font>";
  $event = new Google_Service_Calendar_Event();
  $event->setSummary($_POST['title']);
  $event->setLocation($_POST['location']);
  $start = new Google_Service_Calendar_EventDateTime();
  $start->setTimeZone('Europe/Amsterdam');
  $start->setDateTime($dt);
  $event->setStart($start);
  $end = new Google_Service_Calendar_EventDateTime();
  $end->setDateTime($dt);
  $event->setEnd($end);
  $createdEvent = $cal->events->insert('f8ov32gchvan0b6a0594r72jng@group.calendar.google.com', $event);
  echo "<br><font size=+1>Event created</font>";

  echo "<hr><br><font size=+1>Already connected</font> (No need to login)";

  }

我还在尝试使用 echo $dt->format("c") . "\n";,它给出的结果与上面相同,只是没有了 .000。 而 Google 日历要求我使用类似这样的时间格式:

$start->setDateTime('2012-10-31T10:00:00.000-05:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2012-10-31T10:25:00.000-05:00');
$event->setEnd($end);

来源:在PHP中创建简单的Google日历事件

我的HTML输入字段:

<input type="datetime-local" name="date1">Date1<br>
<input type="datetime-local" name="date2">Date2<br>

目前我没有使用我的 $_POST,我正在检查

$dt = new DateTime();
echo $dt->format("Y-m-d\TH:i:s.000+01:00") . "\n";

所以我的问题是,我如何使用PHP获取Google时间格式?
谢谢。
1个回答

4

查看Google日历API文档,所需格式必须遵循RFC 3339格式。要将DateTime转换为此格式,可以使用DateTime::format函数与DateTime::RFC3339常量配合使用。

您应该得到类似于以下内容:

$dt = new \DateTime();
$calendarDateTime = new \Google_Service_Calendar_EventDateTime();
$calendarDateTime->setDateTime($dt->format(\DateTime::RFC3339));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接