Using php to add an all days event in Google Calendar
Following example is “Creating single-occurrence events” from Google web site.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | function createEvent ($client, $title = 'Tennis with Beth', $desc='Meet for a quick lesson', $where = 'On the courts', $startDate = '2008-01-20', $startTime = '10:00', $endDate = '2008-01-20', $endTime = '11:00', $tzOffset = '-08') { $gdataCal = new Zend_Gdata_Calendar($client); $newEvent = $gdataCal->newEventEntry(); $newEvent->title = $gdataCal->newTitle($title); $newEvent->where = array($gdataCal->newWhere($where)); $newEvent->content = $gdataCal->newContent("$desc"); $when = $gdataCal->newWhen(); $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00"; $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00"; $newEvent->when = array($when); // Upload the event to the calendar server // A copy of the event as it is recorded on the server is returned $createdEvent = $gdataCal->insertEvent($newEvent); return $createdEvent->id->text; } createEvent($client, 'New Years Party', 'Ring in the new year with Kim and I', 'Our house', '2006-12-31', '22:00', '2007-01-01', '03:00', '-08' ); |
How about an all day event? From above example, I can’t simply change start time and end time fields to empty string. It will fail.
So, just modified few lines of createEvent function. Like below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | function createEvent ($client, $title = 'Tennis with Beth', $desc='Meet for a quick lesson', $where = 'On the courts', $startDate = '2008-01-20', $startTime = '10:00', $endDate = '2008-01-20', $endTime = '11:00', $tzOffset = '-08') { $gdataCal = new Zend_Gdata_Calendar($client); $newEvent = $gdataCal->newEventEntry(); $newEvent->title = $gdataCal->newTitle($title); $newEvent->where = array($gdataCal->newWhere($where)); $newEvent->content = $gdataCal->newContent("$desc"); $when = $gdataCal->newWhen(); if ($startTime != "") { $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00"; } else { $when->startTime = "{$startDate}"; } if ($endTime != "") { $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00"; } else { list($yy, $mm, $dd) = explode('-', $endDate); $newdate = mktime(0, 0, 0, $mm, $dd + 1, $yy); $endDate = date('Y-m-d', $newdate); $when->endTime = "{$endDate}"; } $newEntry->when = array($when); // Upload the event to the calendar server // A copy of the event as it is recorded on the server is returned $createdEvent = $gdataCal->insertEvent($newEvent); return $createdEvent->id->text; } createEvent($client, 'New Years Party', 'Ring in the new year with Kim and I', 'Our house', '2006-12-31', '', '2007-01-01', '', '' ); |
After run it, that event will be updated on 12/31/2006 and 1/1/2007.