Correct date formatting for a Google Calendar Event

烈酒焚心 提交于 2020-01-24 09:44:05

问题


I'm trying to create an event for Google Calendar and I'm getting this error :

Invalid value for: "T" found, can only parse bare date string: 2013-08-22T16:00:00

I also tried adding the timezone offset to my string but I set the timezone manually in the EventDateTime object and according to the documentation it's not neccesary.

Here is how I create my time string :

data['start'] = $("#inputDateStart").val() + "T" + $("#inputTimeStart").val();

And how I set this string in my object

$start = new Google_EventDateTime();
$start->setTimeZone('America/Montreal');
$start->setDateTime($data['start']);
$event->setStart($start);

What am I missing? All day events work fine when I just set my date using the setDate function.


回答1:


I dont know your whole code, but I was getting the same error message when trying to set a date-time on a Google calendar event. When I used the Google online api checker, the date/time format and timezone I was using worked just fine. In the end I found that the problem was because I had done this (using something similar to your notation):

$start = new Google_EventDateTime();
$start->setDate('2013-10-10');
$event->setStart($start);

And then later after finding I had both date and time data available:

$start->setTimeZone('Europe/London');
$start->setDateTime('2013-10-10T19:15:00');
$event->setStart($start);

This gave me the error message you mention. Maybe the Google parser thought it was a day event only and then was confused by additional date/time data. The problem went away when I checked first if I had both date and time data and just did:

$start = new Google_EventDateTime();
$start->setTimeZone('Europe/London');
$start->setDateTime('2013-10-10T19:15:00');
$event->setStart($start);



回答2:


You could simply append the timezone at the end of the time:

$start = new Google_EventDateTime();
$start->setDateTime('2011-06-03T10:00:00-07:00');
$event->setStart($start);

This is GMT-7

So in your case this would work:

$start = new Google_EventDateTime();
$start->setDateTime($data['start']."-4:00");
$event->setStart($start);

:)

Though googles own documentation is old and invalid, you may like to use some of their methods: https://developers.google.com/google-apps/calendar/v3/reference/events/insert



来源:https://stackoverflow.com/questions/18313736/correct-date-formatting-for-a-google-calendar-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!