Last active
June 20, 2022 12:09
-
-
Save runezero/cd96a0c5d896a95fc6f4b98046ad5960 to your computer and use it in GitHub Desktop.
[Download Calendar file] Download a calendar file according to given parameters #tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| include('ICS.php'); | |
| date_default_timezone_set('Europe/Amsterdam'); | |
| $date = "18-06-2022"; | |
| $time = "15:30:00"; | |
| $startTime = $date . " " . $time; | |
| if (date('I', strtotime($startTime))) { | |
| $timestamp = strtotime($startTime) - (60*60*2); //minus 2 hours for server time for summer time | |
| $startTime = date('Y-m-d H:i:s', $timestamp); | |
| $endTime = date('Y-m-d H:i:s', $timestamp + (60*60)); //one hour later | |
| //echo 'We are in DST!'; | |
| } else { | |
| $timestamp = strtotime($startTime) - (60*60); | |
| $startTime = date('Y-m-d H:i:s', $timestamp); | |
| $endTime = date('Y-m-d H:i:s', $timestamp + (60*60)); //one hour later | |
| //echo 'We are not in DST!'; | |
| } | |
| $desc_array = ["Array with data, one item per line", "Second line text"]; | |
| $desc = implode("\\n", $desc_array); | |
| header('Content-Type: text/calendar; charset=utf-8'); | |
| $filename = "new_ics_file_" . time(); | |
| header("Content-Disposition: inline; filename=". $filename .".ics"); | |
| $ics = new ICS(array( | |
| 'location' => "Korenbloemstraat 13 5409AX Odiliapeel", | |
| 'description' => $desc, | |
| 'dtstart' => $startTime, | |
| 'dtend' => $endTime, | |
| 'summary' => "The appointment title", | |
| )); | |
| echo $ics->to_string('Europe/Amsterdam'); | |
| die; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //https://gist.github.com/jakebellacera/635416 | |
| /** ICS.php | |
| * ============================================================================= | |
| * Use this class to create an .ics file. | |
| * | |
| * | |
| * Usage | |
| * ----------------------------------------------------------------------------- | |
| * Basic usage - generate ics file contents (see below for available properties): | |
| * $ics = new ICS($props); | |
| * $ics_file_contents = $ics->to_string(); | |
| * | |
| * Setting properties after instantiation | |
| * $ics = new ICS(); | |
| * $ics->set('summary', 'My awesome event'); | |
| * | |
| * You can also set multiple properties at the same time by using an array: | |
| * $ics->set(array( | |
| * 'dtstart' => 'now + 30 minutes', | |
| * 'dtend' => 'now + 1 hour' | |
| * )); | |
| * | |
| * Available properties | |
| * ----------------------------------------------------------------------------- | |
| * description | |
| * String description of the event. | |
| * dtend | |
| * A date/time stamp designating the end of the event. You can use either a | |
| * DateTime object or a PHP datetime format string (e.g. "now + 1 hour"). | |
| * dtstart | |
| * A date/time stamp designating the start of the event. You can use either a | |
| * DateTime object or a PHP datetime format string (e.g. "now + 1 hour"). | |
| * location | |
| * String address or description of the location of the event. | |
| * summary | |
| * String short summary of the event - usually used as the title. | |
| * url | |
| * A url to attach to the the event. Make sure to add the protocol (http:// | |
| * or https://). | |
| */ | |
| class ICS { | |
| const DT_FORMAT = 'Ymd\THis\Z'; | |
| protected $properties = array(); | |
| private $available_properties = array( | |
| 'description', | |
| 'dtend', | |
| 'dtstart', | |
| 'location', | |
| 'summary', | |
| 'url' | |
| ); | |
| public function __construct($props) { | |
| $this->set($props); | |
| } | |
| public function set($key, $val = false) { | |
| if (is_array($key)) { | |
| foreach ($key as $k => $v) { | |
| $this->set($k, $v); | |
| } | |
| } else { | |
| if (in_array($key, $this->available_properties)) { | |
| $this->properties[$key] = $this->sanitize_val($val, $key); | |
| } | |
| } | |
| } | |
| public function to_string($timezone) { | |
| $rows = $this->build_props($timezone); | |
| return implode("\r\n", $rows); | |
| } | |
| private function build_props($timezone) { | |
| // Build ICS properties - add header | |
| $ics_props = array( | |
| 'BEGIN:VCALENDAR', | |
| 'VERSION:2.0', | |
| 'PRODID:-//hacksw/handcal//NONSGML v1.0//EN', | |
| 'CALSCALE:GREGORIAN', | |
| 'X-WR-TIMEZONE:' . $timezone, | |
| 'BEGIN:VEVENT' | |
| ); | |
| // Build ICS properties - add header | |
| $props = array(); | |
| foreach($this->properties as $k => $v) { | |
| $props[strtoupper($k . ($k === 'url' ? ';VALUE=URI' : ''))] = $v; | |
| } | |
| // Set some default values | |
| $props['DTSTAMP'] = $this->format_timestamp('now'); | |
| $props['UID'] = uniqid(); | |
| // Append properties | |
| foreach ($props as $k => $v) { | |
| $ics_props[] = "$k:$v"; | |
| } | |
| // Build ICS properties - add footer | |
| $ics_props[] = 'END:VEVENT'; | |
| $ics_props[] = 'END:VCALENDAR'; | |
| return $ics_props; | |
| } | |
| private function sanitize_val($val, $key = false) { | |
| switch($key) { | |
| case 'dtend': | |
| case 'dtstamp': | |
| case 'dtstart': | |
| $val = $this->format_timestamp($val); | |
| break; | |
| default: | |
| $val = $this->escape_string($val); | |
| } | |
| return $val; | |
| } | |
| private function format_timestamp($timestamp) { | |
| $dt = new DateTime($timestamp); | |
| return $dt->format(self::DT_FORMAT); | |
| } | |
| private function escape_string($str) { | |
| return preg_replace('/([\,;])/','\\\$1', $str); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment