php 动态创建 .ics 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5750249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create .ics file dynamically
提问by iosfreak
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.
我为客户制作了一个网站,他们可以在其中发布活动。与从 iCal 为每个事件手动创建 .ics 文件并上传它不同,我认为最好将其从数据库中拉出并使用 PHP 自动创建一个 .ics 文件。
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:
这是创建我的 .ics 文件的代码:
//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?
现在如何将数据库中的数据转换为正确的时间/日期戳?
采纳答案by afuzzyllama
iCal times stored in UTC and not in the time zone that they originated in unless otherwise specified (see edit)
除非另有说明,否则 iCal 时间存储在 UTC 而不是它们起源的时区(请参阅编辑)
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)
并移动到时间戳格式。您将从 4(?) 列减少到 1 列。为什么这很好?因为您可以使用 PHP date()将日期格式化为您所需要的格式!(我相信你需要使用 strtotime() 并且有人在评论中有一个时区转换功能)
Looking at what needs to be done from an iCal perspective:
从 iCal 的角度来看需要做什么:
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.
http://tools.ietf.org/html/rfc5545#section-3.2.19
编辑:
Z 代表 UTC 时间或绝对时间。
您可以在文件中设置时区并将转换留给日历程序。
http://tools.ietf.org/html/rfc5545#section-3.2.19