如何使用PHP生成给定日期范围和时间的.ics文件

时间:2022-11-23 08:26:27

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代码来创建这个特性吗?

1 个解决方案

#1


23  

http://web.archive.org/web/20120419230026/http://jamiebicknell.tumblr.com/post/413492676/ics-generator-php-class

http://web.archive.org/web/20120419230026/http:/ /jamiebicknell.tumblr.com/post/413492676/ics-generator-php-class

Note: original blog post is gone; preserving with arhcive.org link.

注意:原来的博客文章不见了;保持与arhcive.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();
?>

#1


23  

http://web.archive.org/web/20120419230026/http://jamiebicknell.tumblr.com/post/413492676/ics-generator-php-class

http://web.archive.org/web/20120419230026/http:/ /jamiebicknell.tumblr.com/post/413492676/ics-generator-php-class

Note: original blog post is gone; preserving with arhcive.org link.

注意:原来的博客文章不见了;保持与arhcive.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();
?>