How to generate .ics file using PHP for a given date range and time
27,916
Note: original blog post is gone; preserving with arhcive.org link.
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
<?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
<?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();
?>
Author by
thoyyu
Updated on August 03, 2022Comments
-
thoyyu almost 2 years
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.
Could any one provide me a sample PHP code to create this feature.
-
thoyyu over 11 yearsCannot access the page using this link:
-
hakre over 11 yearswho is the author of the code and under which licensing terms is it available? The website you linked does not work any longer.
-
m4t1t0 over 11 yearsFor me the site works perfect, on the other hand this is the author twitter.com/jamiebicknell ask him.
-
Steve Ives over 8 yearsSorry for resurrecting an old post, but I can't see how the section described as "Output the ICS file to the browser and give the user the option to open or save", allows the user to open or save.
-
Alex G over 7 yearsUsing other non-maintained resource is not a good solution...
-
degenerate over 7 years@SteveIves the part of the header that says
Content-Disposition: attachment;
is what tells the browser it's a file download. That is what OP meant by "allows the user to open or save". I also updated the answer with a link to an archive.org snapshot of the blog post, since it's gone now.