Create iCalendar file using PHP

Asked

Viewed 953 times

1

Wanted to create iCalendar file (extension .ics) to save and reload the file contents to a PHP calendar. Save days, events, start and end of each event, etc. What is the format of this file type, and how to create one with PHP?

1 answer

2

from https://stackoverflow.com/a/12741300/1167333

<?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 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" , "Este é um evento feito por Jamie Bicknell", "GU1 1AA");
$event->show();
? >

Save the ICS file to the server in the current working directory

<?php
$event = new ICS ( "2009-11-06 09:00", "2009-11-06 21:00" , "Test Event" , "Este é um evento feito por Jamie Bicknell", "GU1 1AA");
$event->save();
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.