27 January, 2011

How to Sync php calendar events to Outlook 2007

I found lots of sites that showed how to sync events from my custom calendar to
Microsoft Outlook 2007 using PHP. But could not make any of it work my way. I
somehow managed to make it work. Microsoft Outlook 2007 uses iCal files, i.e.
Internet Calendar files, so with the help of some sites searched using Google
I created an iCal file adding my customs calendar events in it.
Here is the code snippet for the same.

$userTimeZoneName = "America/New_York";
$userTimeZoneAbbr = "GMT";
$eventSummary = "This is summary of event";

$eventStartDate = date("Ymd", time()); //like 20110112
$eventStartTime = date("His", time()); // like 163000
$eventEndDate = "20110128"; //you can set your own end date in Ymd format
$eventEndTime = "1223000"; //you can set your own end time in His format

$eventDescription = "Some event description here";
$eventsLocation = "Nepal, Kathmandu";
$eventId = "20"; //this can be your event id


$ical = "BEGIN:VCALENDAR\n";
$ical .= "PRODID:-//Sudhir/SudhirWebCal//NONSGML v1.0//EN\n";
$ical .= "VERSION:2.0\n";
$ical .="CALSCALE:GREGORIAN\n";
$ical .="METHOD:PUBLISH\n";
$ical .="X-WR-CALNAME:SudhirCal\n";
$ical .="X-WR-TIMEZONE:".$userTimeZoneName."\n";
$ical .="X-WR-CALDESC:Commonfig Events\n";
$ical .="X-PUBLISHED-TTL:PT5M\n";
$ical .="BEGIN:VTIMEZONE\n";
$ical .="TZID:".$userTimeZoneName."\n";
$ical .="X-LIC-LOCATION:".$userTimeZoneName."\n";
$ical .="BEGIN:DAYLIGHT\n";
$ical .="TZOFFSETFROM:+0000\n";
$ical .="TZOFFSETTO:+0100\n";
$ical .="TZNAME:".$userTimeZoneAbbr."\n";
$ical .="DTSTART:19700329T010000\n";
$ical .="RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\n";
$ical .="END:DAYLIGHT\n";
$ical .="BEGIN:STANDARD\n";
$ical .="TZOFFSETFROM:+0100\n";
$ical .="TZOFFSETTO:+0000\n";
$ical .="TZNAME:GMT\n";
$ical .="DTSTART:19701025T020000\n";
$ical .="RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\n";
$ical .="END:STANDARD\n";
$ical .="END:VTIMEZONE\n";
$ical .="BEGIN:VEVENT\n";
$ical .="SUMMARY:".$eventSummary."\n";
$ical .="DTSTART:".$eventStartDate."T".$eventStartTime."\n";
$ical .="DTEND:".$eventEndDate."T".$eventEndTime."\n";
$ical .="DESCRIPTION;ENCODING=QUOTED-PRINTABLE:".str_replace("\r", "=0D=0A",$eventDescription)."\n";
$ical .= "LOCATION:".$eventsLocation."\n";
$ical .="UID:".sha1($eventStartDate.$eventId)."example.com\n";
$ical .="LAST-MODIFIED:20110119T123850Z\n";
$ical .="STATUS:PRIVATE\n";
$ical .="END:VEVENT\n";
$ical .="END:VCALENDAR";

//the line X-PUBLISHED-TTL:PT5M; sets the outlook calendar refresh time to 5 minutes
//so all your events will be automatically synchronized
//to your outlook calendar every 5 minutes t
//this can be increased to 2 hours like X-PUBLISHED-TTL:PT120M;

echo $ical;

//this will output the events in outlook calendar format
?>
The above code can be added inside a function and can be accessed using a link like
webcal://yourdomain.com/your_function_for_ical
In this way we can create calendar files for outlook 2007 and synchronize events of
custom PHP calendar with Microsoft Outlook 2007 calendar.
Hope it helps someone.

No comments: