How to create allday events with Google Calendar A

2019-07-20 09:11发布

I have this code:

$event = new Event();
$event->setSummary( $_event['summary'] );
$event->setLocation( $_event['location'] );
$start = new EventDateTime();
$start->allDay = true;
$start->setDateTime($_event['start']);
$event->setStart($start);
$cal->events->insert( $_calendar['id'], $event  );

But not works =\

Any idea?

1条回答
霸刀☆藐视天下
2楼-- · 2019-07-20 09:42

Use $start->setDate() instead of $start->setDateTime() if you want to create a day long event.

To create a day long event

$event = new Event();
$event->setSummary("Summary of the event");
$event->setLocation("Location");

$start = new EventDateTime();
$start->setDate("2013-01-01"); 
$event->start=$start;

$end = new EventDateTime();
$end->setDate("2013-01-02");
$event->end=$end;

$calendarId="primary"; // primary represents your calendar
$createdEvent = $cal->events->insert($calendarId, $event);

To create a event in a given duration

$event = new Event();
$event->setSummary("Summary of the event");
$event->setLocation("Location");

$start = new EventDateTime();
$start->setDateTime("2013-01-01T0:00:00.000+00:00");
$event->start=$start;

$end = new EventDateTime();
$end->setDateTime("2013-01-01T0:01:00.000+00:00");
$event->end=$end;

$calendarId="primary";
$createdEvent = $cal->events->insert($calendarId, $event);
查看更多
登录 后发表回答