Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

php - Create .ics file dynamically

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
";
echo "PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN
";
echo "VERSION:2.0
";
echo "METHOD:PUBLISH
";
echo "X-MS-OLK-FORCEINSPECTOROPEN:TRUE
";
echo "BEGIN:VEVENT
";
echo "CLASS:PUBLIC
";
echo "CREATED:20091109T101015Z
";

echo "DESCRIPTION:Speaker: $event_query_row[speaker_name]\n\nTopic: $event_query_row[speaker_topic]
";
echo "DTEND:20100208T040000Z
";
echo "DTSTAMP:20100109T093305Z
";
echo "DTSTART:20100208T003000Z
";
echo "LAST-MODIFIED:20091109T101015Z
";
echo "LOCATION:$event_query_row[location]
";
echo "PRIORITY:5
";
echo "SEQUENCE:0
";
echo "SUMMARY;LANGUAGE=en-us:ADAM-Houston Event
";
echo "TRANSP:OPAQUE
";
echo "UID:040000008200E00074C5B7101A82E008000000008062306C6261CA01000000000000000
";
echo "X-MICROSOFT-CDO-BUSYSTATUS:BUSY
";
echo "X-MICROSOFT-CDO-IMPORTANCE:1
";
echo "X-MICROSOFT-DISALLOW-COUNTER:FALSE
";
echo "X-MS-OLK-ALLOWEXTERNCHECK:TRUE
";
echo "X-MS-OLK-AUTOFILLLOCATION:FALSE
";
echo "X-MS-OLK-CONFTYPE:0
";
//Here is to set the reminder for the event.
echo "BEGIN:VALARM
";
echo "TRIGGER:-PT1440M
";
echo "ACTION:DISPLAY
";
echo "DESCRIPTION:Reminder
";
echo "END:VALARM
";
echo "END:VEVENT
";
echo "END:VCALENDAR
";

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
";  // Time iCal item was made
 echo "DTSTART:<year><month><day>T<hour><minutes><seconds>Z
";  // Start time
 echo "DTEND:<year><month><day>T<hour><minutes><seconds>Z
";    // 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...