2015-12-16 160 views
2

我正試圖讓Google Calendar Api正常工作。如果沒有使用我的$ _POST日期時間,它可以完美工作。但是,那麼日期就像谷歌那樣硬編碼,這不是我想要的。當我用我的$ _POST瓦爾它表明我這個錯誤:Google Calendar API日期時間

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

我的日期時間代碼:

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('[email protected]', $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試驗。 谷歌日曆要求我有一個像一角錢時間格式:

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

來源:Create Simple Google Calendar Event in PHP

我的HTML輸入字段:

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

ATM我沒有使用我的$ _ POST我」 m檢查

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

所以我的問題是,我該怎麼辦ogle與PHP的時間格式?

謝謝

回答

1

Google Calendar API docs尋找所需要的格式必須遵循RFC 3339格式。要將DateTime轉換爲此格式,可以將DateTime::format函數與DateTime::RFC3339常數結合使用。

你應該得到的東西,如:

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