Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create .ics file dynamically

Tags:

php

icalendar

I made a website for a client where they can post events. Instead of manually creating .ics files from iCal for every event and uploading it, I though it would be better to pull it out of the database and automatically create a .ics file automatically with PHP.

I can pull information from the database (no problem), but when converting it to a time stamp for the calendar file it a tough one. Here's what I store in the database:

Month: 05 Day: 02 Year: 2011 Time: 11:30am - 1:30pm 

Here's the code to create my .ics files:

//This is the most important coding. header("Content-Type: text/Calendar"); header("Content-Disposition: inline; filename=adamhouston_$id.ics");  echo "BEGIN:VCALENDAR\n"; echo "PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN\n"; echo "VERSION:2.0\n"; echo "METHOD:PUBLISH\n"; echo "X-MS-OLK-FORCEINSPECTOROPEN:TRUE\n"; echo "BEGIN:VEVENT\n"; echo "CLASS:PUBLIC\n"; echo "CREATED:20091109T101015Z\n";  echo "DESCRIPTION:Speaker: $event_query_row[speaker_name]\\n\\nTopic: $event_query_row[speaker_topic]\n"; echo "DTEND:20100208T040000Z\n"; echo "DTSTAMP:20100109T093305Z\n"; echo "DTSTART:20100208T003000Z\n"; echo "LAST-MODIFIED:20091109T101015Z\n"; echo "LOCATION:$event_query_row[location]\n"; echo "PRIORITY:5\n"; echo "SEQUENCE:0\n"; echo "SUMMARY;LANGUAGE=en-us:ADAM-Houston Event\n"; echo "TRANSP:OPAQUE\n"; echo "UID:040000008200E00074C5B7101A82E008000000008062306C6261CA01000000000000000\n"; echo "X-MICROSOFT-CDO-BUSYSTATUS:BUSY\n"; echo "X-MICROSOFT-CDO-IMPORTANCE:1\n"; echo "X-MICROSOFT-DISALLOW-COUNTER:FALSE\n"; echo "X-MS-OLK-ALLOWEXTERNCHECK:TRUE\n"; echo "X-MS-OLK-AUTOFILLLOCATION:FALSE\n"; echo "X-MS-OLK-CONFTYPE:0\n"; //Here is to set the reminder for the event. echo "BEGIN:VALARM\n"; echo "TRIGGER:-PT1440M\n"; echo "ACTION:DISPLAY\n"; echo "DESCRIPTION:Reminder\n"; echo "END:VALARM\n"; echo "END:VEVENT\n"; echo "END:VCALENDAR\n"; 

Now how do I convert the data in my database to a correct time/date stamp?

like image 300
iosfreak Avatar asked Apr 21 '11 22:04

iosfreak


People also ask

How do I create an ICS file with multiple events?

iCal/Calendar (Mac)Enter all the event's information in the popup, (title, location, time(s), etc.) and click anywhere outside of the popup to save the details. Then, simply drag and drop the newly-created event on to your desktop to automatically create the . ics file.

How do I automatically open an ICS file?

By clicking on “Select file from your computer”, you can search your computer for the ICS file, select it, open it and automatically enter the event by choosing “Import”.


1 Answers

iCal times stored in UTC and not in the time zone that they originated in unless otherwise specified (see edit)

If you want your code to be generic, you'll need to store the time zone as well. If you really want to... you can hard code in the conversion.

I would also recommend not storing time in this format:

 Month: 05  Day: 02   Year: 2011  Time: 11:30am - 1:30pm 

and move to a timestamp format. You'll go from 4(?) columns down to one. Why is this good? Because you can use PHP date() to format the date for you into exactly what you need! (you'll need to use strtotime() I believe and also someone has a timezone conversion function in the comments)

Looking at what needs to be done from an iCal perspective:

 echo "DTSTAMP:<year><month><day>T<hour><minutes><seconds>Z\n";  // Time iCal item was made  echo "DTSTART:<year><month><day>T<hour><minutes><seconds>Z\n";  // Start time  echo "DTEND:<year><month><day>T<hour><minutes><seconds>Z\n";    // End time 

EDIT:
The Z represents UTC time, or absolute time.
You can set the timezone in the file and leave the conversion to the calendar program.
https://www.rfc-editor.org/rfc/rfc5545#section-3.2.19

like image 51
afuzzyllama Avatar answered Oct 07 '22 19:10

afuzzyllama