简体   繁体   中英

why Google Calendar all day events return start date as today with php

I am using Google calendar api V3 with PHP and am trying to get event's summary, description and start date. My issue is with the last element: Start date. When events in the calendar are set as "All day" events, the response from the following code gives me today's date and the exact time when the query was made:

echo "<div id=Activite class=titre>", $event->getSummary(), "</div>\n";

$event_date = (new DateTime($event->getStart()->getDateTime()))->format('d/m/Y H:i');
echo "<div class=date_start><span style=color:yellow;>Start: </span>", $event_date, "</div>\n";

Here is an example of what is returned:

prise de photos des élèves
Start: 22/11/2014 18:30

Reading the same output from the "Google APIs Explorer", I get:

start": {
"date": "2013-09-13"

Which is nothing like what I am getting in my case. What am I doing wrong?

Well, here goes for the answer. After great advise by "ippi", I got to understand that Google uses 2 different variables for an event: start... "Date" / "DateTime". The code that worked for me is this work around that enabled the code to recognize if it was dealing with an all day event or not:

if (($event->getStart()->getDate())!= NULL) {
    $event_date = (new DateTime($event->getStart()->getDate()))->format('d/m/Y');
} else {
    $event_date = (new DateTime($event->getStart()->getDateTime()))->format('d/m/Y H:i');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM