iCalendar files are standard format supported in all calendars. Generally these files contain information such as title, description, start time, end time, description  about an event. This can be imported to any standard calendar such as Google calendar. Lets create an ics file generator in php.

<?php
    class ICSGenerator {
        var $data;
        var $name;
        function __construct($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))."\nORGANIZER;CN=ashish.bigbos@gmail.com:mailto:ashish.bigbos@gmail.com\nLOCATION:".$location."\nTRANSP: OPAQUE\nSEQUENCE:0\nUID:\nDTSTAMP:".date("Ymd\THis\Z")."\nSUMMARY:".$name."\nDESCRIPTION:".$description."\nPRIORITY:5\nCLASS:PUBLIC\nBEGIN:VALARM\nTRIGGER:-PT10M\nACTION:DISPLAY\nDESCRIPTION:Reminder\nEND:VALARM\nEND:VEVENT\nEND:VCALENDAR\n";
        }
        public function save() {
            file_put_contents("saved-ics/".$this->name.".ics",$this->data);
        }
    }
?>

This class can be use to create a standard ics file. The final output will be stored to saved-ics/ folder, you can change it according to your need. During object creation we will pass following parameters,

  • Start Time
  • End Time
  • Event Name
  • Description (Support HTML  inputs)
  • Location

Let’s understand this with an example,

<?php
	require_once("ics_generator.php");
	$start_time = "2021-07-27 11:00";
	$end_time = "2021-07-27 11:30";
	$title = "Example Event";
	$description = "<b>Agenda:</b><br><ul><li>Lorem ipsum dolar shit ame is dummy text</li><li>Lorem ipsum dolar shit ame is dummy text</li><li>Lorem ipsum dolar shit ame is dummy text</li></ul>";
	$location = "https://meet.google.com/bdf-kxrv-vps";

	$event = new ICS($start_time,$end_time,$title,$description,$location);
	$event->save();
?>

Here start_time and end_time are in UTC time which further converts to your local timezone once imported to any calendar. You can see the description is in html format. Location can be anything like event place or url if its a online event or meeting. For reference I have imported the same event in my google calendar.

Thanks for reading! If you have any doubt regarding this feel free to add in the comment.

4 Replies to “How to create ics (iCalendar) file in PHP?”

Leave a Reply

Your email address will not be published. Required fields are marked *