Last active
August 23, 2021 16:57
-
-
Save kaelri/eb1f4b209457d272bf9783a6b6d08c7e to your computer and use it in GitHub Desktop.
Create ICS calendar event file from data.
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
| <?php | |
| function build_ical( array $input, string $output_path ) { | |
| $template = file_get_contents( __DIR__ . '/template.ics' ); | |
| $timezone = new DateTimeZone('America/New_York'); | |
| $now = new DateTime( 'now', $timezone ); | |
| $start = new DateTime( $input['start'], $timezone ); | |
| $end = new DateTime( $input['end'], $timezone ); | |
| $ics = sprintf( $template, | |
| $end->format('Ymd\THis'), | |
| md5( $input['name'] ), | |
| $now->format('Ymd\THis\Z'), | |
| $input['url'], | |
| $input['desc'], | |
| $input['url'], | |
| $input['name'], | |
| $now->format('Ymd\THis\Z'), | |
| $now->format('Ymd\THis\Z'), | |
| $start->format('Ymd\THis') | |
| ); | |
| file_put_contents( $output_path, $ics ); | |
| } | |
| $example = [ | |
| 'name' => 'My Event', | |
| 'start' => '2022-07-10 14:00', | |
| 'end' => '2022-07-10 16:00', | |
| 'desc' => 'This is just the greatest event that ever did event.', | |
| 'url' => 'https://www.google.com/' | |
| ]; | |
| build_ical( $example, 'example.ics' ); |
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
| BEGIN:VCALENDAR | |
| VERSION:2.0 | |
| CALSCALE:GREGORIAN | |
| BEGIN:VTIMEZONE | |
| TZID:America/New_York | |
| BEGIN:DAYLIGHT | |
| TZOFFSETFROM:-0500 | |
| RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU | |
| DTSTART:20070311T020000 | |
| TZNAME:EDT | |
| TZOFFSETTO:-0400 | |
| END:DAYLIGHT | |
| BEGIN:STANDARD | |
| TZOFFSETFROM:-0400 | |
| RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU | |
| DTSTART:20071104T020000 | |
| TZNAME:EST | |
| TZOFFSETTO:-0500 | |
| END:STANDARD | |
| END:VTIMEZONE | |
| BEGIN:VEVENT | |
| TRANSP:OPAQUE | |
| DTEND;TZID=America/New_York;VALUE=DATE:20220710T160000 | |
| UID:324f6d8407d6852c3f191d929811cdc5 | |
| DTSTAMP:20210823T120717Z | |
| LOCATION:https://www.google.com/ | |
| DESCRIPTION:This is just the greatest event that ever did event. | |
| SEQUENCE:1 | |
| X-APPLE-TRAVEL-ADVISORY-BEHAVIOR:AUTOMATIC | |
| URL;VALUE=URI:https://www.google.com/ | |
| SUMMARY:My Event | |
| LAST-MODIFIED:20210823T120717Z | |
| CREATED:20210823T120717Z | |
| DTSTART;TZID=America/New_York;VALUE=DATE:20220710T140000 | |
| END:VEVENT | |
| END:VCALENDAR |
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
| BEGIN:VCALENDAR | |
| VERSION:2.0 | |
| CALSCALE:GREGORIAN | |
| BEGIN:VTIMEZONE | |
| TZID:America/New_York | |
| BEGIN:DAYLIGHT | |
| TZOFFSETFROM:-0500 | |
| RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU | |
| DTSTART:20070311T020000 | |
| TZNAME:EDT | |
| TZOFFSETTO:-0400 | |
| END:DAYLIGHT | |
| BEGIN:STANDARD | |
| TZOFFSETFROM:-0400 | |
| RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU | |
| DTSTART:20071104T020000 | |
| TZNAME:EST | |
| TZOFFSETTO:-0500 | |
| END:STANDARD | |
| END:VTIMEZONE | |
| BEGIN:VEVENT | |
| TRANSP:OPAQUE | |
| DTEND;TZID=America/New_York;VALUE=DATE:%s | |
| UID:%s | |
| DTSTAMP:%s | |
| LOCATION:%s | |
| DESCRIPTION:%s | |
| SEQUENCE:1 | |
| URL;VALUE=URI:%s | |
| SUMMARY:%s | |
| LAST-MODIFIED:%s | |
| CREATED:%s | |
| DTSTART;TZID=America/New_York;VALUE=DATE:%s | |
| END:VEVENT | |
| END:VCALENDAR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment