On a project I was working on recently it would have been cool if we could dynamically create an ICS file so people could download and import events into their calendars. I did some looking and found several “scripts” that all claimed to do the trick, but none worked quite right. I kind of pieced them together and came up with this.
<?php header("Content-Type: text/Calendar"); header("Content-Disposition: inline; filename=calendar.ics"); echo "BEGIN:VCALENDARn"; echo "PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//ENn"; echo "VERSION:2.0n"; echo "METHOD:PUBLISHn"; echo "X-MS-OLK-FORCEINSPECTOROPEN:TRUEn"; echo "BEGIN:VEVENTn"; echo "CLASS:PUBLICn"; echo "CREATED:$createdn"; echo "DESCRIPTION:Description of the Event Goes Heren"; echo "DTSTAMP:20120715T190000n"; echo "DTSTART:20120715T190000n"; echo "DTEND:20120715T210000n"; echo "LAST-MODIFIED:0101T000000n"; echo "LOCATION:Location_of_Eventn"; echo "PRIORITY:5n"; echo "SEQUENCE:0n"; echo "SUMMARY;LANGUAGE=en-us:Summary Messagen"; echo "TRANSP:OPAQUEn"; echo "UID:$random_numbern"; // UID just needs to be some random number. I used rand() in PHP. echo "X-MICROSOFT-CDO-BUSYSTATUS:BUSYn"; echo "X-MICROSOFT-CDO-IMPORTANCE:1n"; echo "X-MICROSOFT-DISALLOW-COUNTER:FALSEn"; echo "X-MS-OLK-ALLOWEXTERNCHECK:TRUEn"; echo "X-MS-OLK-AUTOFILLLOCATION:FALSEn"; echo "X-MS-OLK-CONFTYPE:0n"; //Here is where you set the reminder for the event. echo "BEGIN:VALARMn"; echo "TRIGGER:-PT1440Mn"; echo "ACTION:DISPLAYn"; echo "DESCRIPTION:Reminder - Reminder Text Goes Heren"; echo "END:VALARMn"; echo "END:VEVENTn"; echo "END:VCALENDARn"; ?>
Lets look at a few things. The header section tells your browser that we’re making an ICS file and it shouldn’t try to display it. In fact, it should bring up a download dialogue so you can save it to your computer.
The next section sets up the file as a Microsoft Outlook file. Seems like, without that, there are issues when you go to import it. Like your calendar needs to know the file is formatted for Outlook.
The biggest section outputs the actual data that shows up in the event. In my application I put this section in a loop to create the events list. The $created variable is simply a date. I used a PHP date function to create the variable. The rest is pretty well documented. Oh, and $random_number is really just a random number. No secret here.
The next section sets up how reminders for the events happen. You can edit the text as well.
The last section should be left alone. It’s just closing out the opening tags above.
Not a great explanation of how it works, but you should be able to copy and paste the code and make it work for you. Hopefully it helps if you need it.