如何将Google_Service_Calendar_EventDateTime对象转换为字符串?

时间:2014-11-20 22:08:28

标签: php google-api-php-client

我正在尝试编写一个简单的php解析器来抓取Google日历事件并打印值。

$apiKey = "worworjdjdidjd";
$calendarID = "20c8uhdghs688s787h@group.calendar.google.com";
$optionalParams = array('maxResults' => '100', 'singleEvents' => 'True', 'orderBy' => 'startTime', 'timeMin' => $dateStart, 'timeMax' => $dateEnd);


$client = new Google_Client();
$client->setApplicationName("CAL");
$client->setDeveloperKey($apiKey);

$service = new Google_Service_Calendar($client);

$events = $service->events->listEvents($calendarID, $optionalParams);


foreach ($events->getItems() as $event) {
   echo "getSummary ". $event->getSummary() . "<br />";
   echo "getStart ". $event->getStart() . "<br />";
}

getStart函数返回一个Google_Service_Calendar_EventDateTime对象。当我试图回应标准时,我得到并且错误:

Catchable fatal error: Object of class Google_Service_Calendar_EventDateTime could not be converted to string 

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

尝试一下:

$event->getStart()->dateTime

答案 1 :(得分:0)

这比这要棘手得多,因为它们是两种类型的事件:全天以及开始和结束,所以您必须同时满足两种情况:

$dateStart = ($event->start->getDate() != '') ? $event->start->getDate() : $event->start->getDateTime();
$dateEnd = ($event->end->getDate() != '') ? $event->end->getDate() : $event->end->getDateTime();
$start = new DateTime($dateStart);
$end = new DateTime($dateEnd);

最后,您有了一个不错的DateTime对象,可以按照自己喜欢的格式进行格式化:

$start->format("d.m.Y H:i");
// 20.12.2019 09:30