如何使用 PHP 为给定的日期范围和时间生成 .ics 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12739247/
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
How to generate .ics file using PHP for a given date range and time
提问by thoyyu
I am trying to find an effective method to generate a downloadable ".ics" file using PHP, based on a given date range (start date - end date) and reminder time.
我正在尝试根据给定的日期范围(开始日期 - 结束日期)和提醒时间,找到一种使用 PHP 生成可下载“.ics”文件的有效方法。
Could any one provide me a sample PHP code to create this feature.
任何人都可以为我提供一个示例 PHP 代码来创建此功能。
回答by m4t1t0
Note:original blog post is gone; preserving with arhcive.org link.
注意:原博文已消失;使用 archcive.org 链接保存。
Copy and paste the information of the above link:
复制粘贴上面链接的信息:
<?php
class ICS {
var $data;
var $name;
function ICS($start,$end,$name,$description,$location) {
$this->name = $name;
$this->data = "BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:PUBLISH\nBEGIN:VEVENT\nDTSTART:".date("Ymd\THis\Z",strtotime($start))."\nDTEND:".date("Ymd\THis\Z",strtotime($end))."\nLOCATION:".$location."\nTRANSP: OPAQUE\nSEQUENCE:0\nUID:\nDTSTAMP:".date("Ymd\THis\Z")."\nSUMMARY:".$name."\nDESCRIPTION:".$description."\nPRIORITY:1\nCLASS:PUBLIC\nBEGIN:VALARM\nTRIGGER:-PT10080M\nACTION:DISPLAY\nDESCRIPTION:Reminder\nEND:VALARM\nEND:VEVENT\nEND:VCALENDAR\n";
}
function save() {
file_put_contents($this->name.".ics",$this->data);
}
function show() {
header("Content-type:text/calendar");
header('Content-Disposition: attachment; filename="'.$this->name.'.ics"');
Header('Content-Length: '.strlen($this->data));
Header('Connection: close');
echo $this->data;
}
}
?>
Output the ICS file to the browser and give the user the option to open or save
将 ICS 文件输出到浏览器,并为用户提供打开或保存的选项
<?php
$event = new ICS("2009-11-06 09:00","2009-11-06 21:00","Test Event","This is an event made by Jamie Bicknell","GU1 1AA");
$event->show();
?>
Save the ICS file onto the server in the current working directory
将 ICS 文件保存到服务器的当前工作目录中
<?php
$event = new ICS("2009-11-06 09:00","2009-11-06 21:00","Test Event","This is an event made by Jamie Bicknell","GU1 1AA");
$event->save();
?>

