Google Calendar API日期时间 [英] Google Calendar API date time

查看:140
本文介绍了Google Calendar API日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Google Calendar 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>

ATM我没有使用我的$ _POST,我正在与

进行检查

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

所以我的问题是,如何使用PHP获取Google时间格式?

谢谢

解决方案

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

您应该得到类似的东西:

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

I'm trying to get the Google Calendar Api to work. Without the use of my $_POST date times it works perfect. But then the date is hard coded like the google ones and that is not what i want. When i use my $_POST vars it shows me this error:

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**.'

My dateTime code:

  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)";

  }

I'm also experimenting with echo $dt->format("c") . "\n"; which is giving me the same thing only without .000 . Google Calendar requires me to have a dime time format like:

$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);

Source: Create Simple Google Calendar Event in PHP

My HTML input fields:

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

ATM i'm not using my $_POST i'm checking with

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

So my question is, How do i get the google time format with PHP?

Thank you

解决方案

Looking at the Google Calendar API docs the required format must follow the RFC 3339 format. To convert a DateTime to this format one can use the DateTime::format function in conjunction with the DateTime::RFC3339 constant.

You should get something like:

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

这篇关于Google Calendar API日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆