Skip to content

Instantly share code, notes, and snippets.

@anylzer
Forked from jakebellacera/ICS.php
Created November 1, 2017 04:13
Show Gist options
  • Select an option

  • Save anylzer/eb6b5e0cb76a1978d85ab3959e934ae6 to your computer and use it in GitHub Desktop.

Select an option

Save anylzer/eb6b5e0cb76a1978d85ab3959e934ae6 to your computer and use it in GitHub Desktop.

Revisions

  1. @jakebellacera jakebellacera revised this gist Jan 17, 2017. 2 changed files with 4 additions and 4 deletions.
    6 changes: 3 additions & 3 deletions ICS.php
    Original file line number Diff line number Diff line change
    @@ -23,14 +23,14 @@
    *
    * 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").
    * description
    * String description of the event.
    * location
    * String address or description of the location of the event.
    * summary
    @@ -45,9 +45,9 @@ class ICS {

    protected $properties = array();
    private $available_properties = array(
    'description',
    'dtend',
    'dtstart',
    'description',
    'location',
    'summary',
    'url'
    2 changes: 1 addition & 1 deletion _PHPtoICS.md
    Original file line number Diff line number Diff line change
    @@ -18,9 +18,9 @@ $ics_file_contents = $ics->to_string();

    ### Available properties

    * **description** - string description of the event.
    * **dtend** - date/time stamp designating the end of the event. You can use either a `DateTime` object or a [PHP datetime format string](http://php.net/manual/en/datetime.formats.php) (e.g. "now + 1 hour").
    * **dtstart** - date/time stamp designating the start of the event. You can use either a `DateTime` object or a [PHP datetime format string](http://php.net/manual/en/datetime.formats.php) (e.g. "now + 1 hour").
    * **description** - string description of the event.
    * **location** - string address or description of the location of the event.
    * **summary** - string short summary of the event - usually used as the title.
    * **url** - string url to attach to the the event. Make sure to add the protocol (`http://` or `https://`).
  2. @jakebellacera jakebellacera revised this gist Jan 16, 2017. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions _PHPtoICS.md
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,19 @@
    # PHP to ICS

    This library contains a convenient script to generate iCalendar (.ics) files on the fly in PHP.
    This gist contains a convenient script to generate iCalendar (.ics) files on the fly in PHP.

    ## Basic usage

    ```php
    include 'ICS.php'

    $props = array(
    $properties = array(
    'dtstart' => 'now',
    'dtend' => 'now + 30 minutes'
    );

    $ics = new ICS($props);
    $ics->to_string();
    $ics = new ICS($properties);
    $ics_file_contents = $ics->to_string();
    ```

    ### Available properties
    @@ -27,7 +27,9 @@ $ics->to_string();

    ## Detailed examples

    ### Button to download an ICS file
    ### Button that downloads an ICS file when clicked

    This example contains a form on the front-end that submits to a PHP script that initiates a download of an ICS file. This example uses hidden form fields to set the properties dynamically.

    **index.html**

  3. @jakebellacera jakebellacera revised this gist Jan 16, 2017. 3 changed files with 197 additions and 70 deletions.
    131 changes: 131 additions & 0 deletions ICS.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,131 @@
    <?php

    /**
    * 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
    * --------------------
    * 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").
    * description
    * String description of the event.
    * 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(
    'dtend',
    'dtstart',
    'description',
    '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() {
    $rows = $this->build_props();
    return implode("\r\n", $rows);
    }

    private function build_props() {
    // Build ICS properties - add header
    $ics_props = array(
    'BEGIN:VCALENDAR',
    'VERSION:2.0',
    'PRODID:-//hacksw/handcal//NONSGML v1.0//EN',
    'CALSCALE:GREGORIAN',
    '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);
    }
    }
    70 changes: 0 additions & 70 deletions PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -1,70 +0,0 @@
    <?php
    // Variables used in this script:
    // $summary - text title of the event
    // $datestart - the starting date (in seconds since unix epoch)
    // $dateend - the ending date (in seconds since unix epoch)
    // $address - the event's address
    // $uri - the URL of the event (add http://)
    // $description - text description of the event
    // $filename - the name of this file for saving (e.g. my-event-name.ics)
    //
    // Notes:
    // - the UID should be unique to the event, so in this case I'm just using
    // uniqid to create a uid, but you could do whatever you'd like.
    //
    // - iCal requires a date format of "yyyymmddThhiissZ". The "T" and "Z"
    // characters are not placeholders, just plain ol' characters. The "T"
    // character acts as a delimeter between the date (yyyymmdd) and the time
    // (hhiiss), and the "Z" states that the date is in UTC time. Note that if
    // you don't want to use UTC time, you must prepend your date-time values
    // with a TZID property. See RFC 5545 section 3.3.5
    //
    // - The Content-Disposition: attachment; header tells the browser to save/open
    // the file. The filename param sets the name of the file, so you could set
    // it as "my-event-name.ics" or something similar.
    //
    // - Read up on RFC 5545, the iCalendar specification. There is a lot of helpful
    // info in there, such as formatting rules. There are also many more options
    // to set, including alarms, invitees, busy status, etc.
    //
    // https://www.ietf.org/rfc/rfc5545.txt

    // 1. Set the correct headers for this file
    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=' . $filename);

    // 2. Define helper functions

    // Converts a unix timestamp to an ics-friendly format
    // NOTE: "Z" means that this timestamp is a UTC timestamp. If you need
    // to set a locale, remove the "\Z" and modify DTEND, DTSTAMP and DTSTART
    // with TZID properties (see RFC 5545 section 3.3.5 for info)
    //
    // Also note that we are using "H" instead of "g" because iCalendar's Time format
    // requires 24-hour time (see RFC 5545 section 3.3.12 for info).
    function dateToCal($timestamp) {
    return date('Ymd\THis\Z', $timestamp);
    }

    // Escapes a string of characters
    function escapeString($string) {
    return preg_replace('/([\,;])/','\\\$1', $string);
    }

    // 3. Echo out the ics file's contents
    ?>
    BEGIN:VCALENDAR
    VERSION:2.0
    PRODID:-//hacksw/handcal//NONSGML v1.0//EN
    CALSCALE:GREGORIAN
    BEGIN:VEVENT
    DTEND:<?= dateToCal($dateend) ?>
    UID:<?= uniqid() ?>
    DTSTAMP:<?= dateToCal(time()) ?>
    LOCATION:<?= escapeString($address) ?>
    DESCRIPTION:<?= escapeString($description) ?>
    URL;VALUE=URI:<?= escapeString($uri) ?>
    SUMMARY:<?= escapeString($summary) ?>
    DTSTART:<?= dateToCal($datestart) ?>
    END:VEVENT
    END:VCALENDAR
    66 changes: 66 additions & 0 deletions _PHPtoICS.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    # PHP to ICS

    This library contains a convenient script to generate iCalendar (.ics) files on the fly in PHP.

    ## Basic usage

    ```php
    include 'ICS.php'

    $props = array(
    'dtstart' => 'now',
    'dtend' => 'now + 30 minutes'
    );

    $ics = new ICS($props);
    $ics->to_string();
    ```

    ### Available properties

    * **dtend** - date/time stamp designating the end of the event. You can use either a `DateTime` object or a [PHP datetime format string](http://php.net/manual/en/datetime.formats.php) (e.g. "now + 1 hour").
    * **dtstart** - date/time stamp designating the start of the event. You can use either a `DateTime` object or a [PHP datetime format string](http://php.net/manual/en/datetime.formats.php) (e.g. "now + 1 hour").
    * **description** - string description of the event.
    * **location** - string address or description of the location of the event.
    * **summary** - string short summary of the event - usually used as the title.
    * **url** - string url to attach to the the event. Make sure to add the protocol (`http://` or `https://`).

    ## Detailed examples

    ### Button to download an ICS file

    **index.html**

    ```html
    <form method="post" action="/download-ics.php">
    <input type="hidden" name="date_start" value="2017-1-16 9:00AM">
    <input type="hidden" name="date_end" value="2017-1-16 10:00AM">
    <input type="hidden" name="location" value="123 Fake St, New York, NY">
    <input type="hidden" name="description" value="This is my description">
    <input type="hidden" name="summary" value="This is my summary">
    <input type="hidden" name="url" value="http://example.com">
    <input type="submit" value="Add to Calendar">
    </form>
    ```

    **download-ics.php**

    ```php
    <?php

    include 'ICS.php';

    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=invite.ics');

    $ics = new ICS(array(
    'location' => $_POST['location'],
    'description' => $_POST['description'],
    'dtstart' => $_POST['date_start'],
    'dtend' => $_POST['date_end'],
    'summary' => $_POST['summary'],
    'url' => $_POST['url']
    ));

    echo $ics->to_string();
    ```
  4. @jakebellacera jakebellacera revised this gist Oct 10, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -67,4 +67,4 @@ function escapeString($string) {
    SUMMARY:<?= escapeString($summary) ?>
    DTSTART:<?= dateToCal($datestart) ?>
    END:VEVENT
    END:VCALENDAR';
    END:VCALENDAR
  5. @jakebellacera jakebellacera revised this gist Oct 10, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -40,10 +40,10 @@
    // to set a locale, remove the "\Z" and modify DTEND, DTSTAMP and DTSTART
    // with TZID properties (see RFC 5545 section 3.3.5 for info)
    //
    // Also note that we are using "g" instead of "H" because iCalendar's Time format
    // Also note that we are using "H" instead of "g" because iCalendar's Time format
    // requires 24-hour time (see RFC 5545 section 3.3.12 for info).
    function dateToCal($timestamp) {
    return date('Ymd\Tgis\Z', $timestamp);
    return date('Ymd\THis\Z', $timestamp);
    }

    // Escapes a string of characters
  6. @jakebellacera jakebellacera revised this gist Aug 28, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -39,6 +39,9 @@
    // NOTE: "Z" means that this timestamp is a UTC timestamp. If you need
    // to set a locale, remove the "\Z" and modify DTEND, DTSTAMP and DTSTART
    // with TZID properties (see RFC 5545 section 3.3.5 for info)
    //
    // Also note that we are using "g" instead of "H" because iCalendar's Time format
    // requires 24-hour time (see RFC 5545 section 3.3.12 for info).
    function dateToCal($timestamp) {
    return date('Ymd\Tgis\Z', $timestamp);
    }
  7. Jake Bellacera revised this gist Jul 23, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,6 @@ function escapeString($string) {

    // 3. Echo out the ics file's contents
    ?>

    BEGIN:VCALENDAR
    VERSION:2.0
    PRODID:-//hacksw/handcal//NONSGML v1.0//EN
  8. Jake Bellacera revised this gist Jul 15, 2013. 1 changed file with 12 additions and 5 deletions.
    17 changes: 12 additions & 5 deletions PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -8,22 +8,26 @@
    // $description - text description of the event
    // $filename - the name of this file for saving (e.g. my-event-name.ics)
    //
    // Notes
    // Notes:
    // - the UID should be unique to the event, so in this case I'm just using
    // uniqid to create a uid, but you could do whatever you'd like.
    //
    // - iCal requires a date format of "yyyymmddThhiissZ". The "T" and "Z"
    // characters are not placeholders, just plain ol' characters.
    // characters are not placeholders, just plain ol' characters. The "T"
    // character acts as a delimeter between the date (yyyymmdd) and the time
    // (hhiiss), and the "Z" states that the date is in UTC time. Note that if
    // you don't want to use UTC time, you must prepend your date-time values
    // with a TZID property. See RFC 5545 section 3.3.5
    //
    // - The Content-Disposition: attachment; header tells the browser to save/open
    // the file. The filename param sets the name of the file, so you could set
    // it as "my-event-name.ics" or something similar.
    //
    // - Read up on RFC2445, the iCalendar specification. There is a lot of helpful
    // - Read up on RFC 5545, the iCalendar specification. There is a lot of helpful
    // info in there, such as formatting rules. There are also many more options
    // to set, including alarms, invitees, busy status, etc.
    //
    // https://www.ietf.org/rfc/rfc2445.txt
    // https://www.ietf.org/rfc/rfc5545.txt

    // 1. Set the correct headers for this file
    header('Content-type: text/calendar; charset=utf-8');
    @@ -32,8 +36,11 @@
    // 2. Define helper functions

    // Converts a unix timestamp to an ics-friendly format
    // NOTE: "Z" means that this timestamp is a UTC timestamp. If you need
    // to set a locale, remove the "\Z" and modify DTEND, DTSTAMP and DTSTART
    // with TZID properties (see RFC 5545 section 3.3.5 for info)
    function dateToCal($timestamp) {
    return date('Ymd\This\Z', $timestamp);
    return date('Ymd\Tgis\Z', $timestamp);
    }

    // Escapes a string of characters
  9. Jake Bellacera revised this gist Jul 15, 2013. 1 changed file with 12 additions and 14 deletions.
    26 changes: 12 additions & 14 deletions PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -25,39 +25,37 @@
    //
    // https://www.ietf.org/rfc/rfc2445.txt

    // 1. Set the corrent headers for this file
    // 1. Set the correct headers for this file
    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=' . $filename);

    // 2. Define helper functions

    // Converts a unix timestamp to an ics-friendly format
    //
    // Returns an ics-formatted timestamp
    function dateToCal($timestamp) {
    return date('Ymd\This\Z', $timestamp);
    }

    // Escapes a string of characters
    //
    // Returns the escaped string
    function escapeString($string) {
    return preg_replace('/([\,;])/','\\\$1', $string);
    }

    // 3. Echo out the ics file's contents
    echo 'BEGIN:VCALENDAR
    ?>

    BEGIN:VCALENDAR
    VERSION:2.0
    PRODID:-//hacksw/handcal//NONSGML v1.0//EN
    CALSCALE:GREGORIAN
    BEGIN:VEVENT
    DTEND:' . dateToCal($dateend) . '
    UID:' . uniqid() . '
    DTSTAMP:' . dateToCal(time()) . '
    LOCATION:' . escapeString($address) . '
    DESCRIPTION:' . escapeString($description) . '
    URL;VALUE=URI: ' . escapeString($uri) . '
    SUMMARY:' . escapeString($summary) . '
    DTSTART:' . dateToCal($datestart) . '
    DTEND:<?= dateToCal($dateend) ?>
    UID:<?= uniqid() ?>
    DTSTAMP:<?= dateToCal(time()) ?>
    LOCATION:<?= escapeString($address) ?>
    DESCRIPTION:<?= escapeString($description) ?>
    URL;VALUE=URI:<?= escapeString($uri) ?>
    SUMMARY:<?= escapeString($summary) ?>
    DTSTART:<?= dateToCal($datestart) ?>
    END:VEVENT
    END:VCALENDAR';
  10. Jake Bellacera revised this gist Jul 15, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@
    //
    // Returns an ics-formatted timestamp
    function dateToCal($timestamp) {
    return date('Ymd\This\Z', $timestamp);
    return date('Ymd\This\Z', $timestamp);
    }

    // Escapes a string of characters
  11. Jake Bellacera revised this gist Jul 15, 2013. 1 changed file with 51 additions and 36 deletions.
    87 changes: 51 additions & 36 deletions PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -1,48 +1,63 @@
    <?php
    // Fetch vars
    $event = array(
    'id' => $_GET['id'],
    'title' => $_GET['title'],
    'address' => $_GET['address'],
    'description' => $_GET['description'],
    'datestart' => $_GET['datestart'],
    'dateend' => $_GET['dateend'],
    'address' => $_GET['stage']
    );
    // Variables used in this script:
    // $summary - text title of the event
    // $datestart - the starting date (in seconds since unix epoch)
    // $dateend - the ending date (in seconds since unix epoch)
    // $address - the event's address
    // $uri - the URL of the event (add http://)
    // $description - text description of the event
    // $filename - the name of this file for saving (e.g. my-event-name.ics)
    //
    // Notes
    // - the UID should be unique to the event, so in this case I'm just using
    // uniqid to create a uid, but you could do whatever you'd like.
    //
    // - iCal requires a date format of "yyyymmddThhiissZ". The "T" and "Z"
    // characters are not placeholders, just plain ol' characters.
    //
    // - The Content-Disposition: attachment; header tells the browser to save/open
    // the file. The filename param sets the name of the file, so you could set
    // it as "my-event-name.ics" or something similar.
    //
    // - Read up on RFC2445, the iCalendar specification. There is a lot of helpful
    // info in there, such as formatting rules. There are also many more options
    // to set, including alarms, invitees, busy status, etc.
    //
    // https://www.ietf.org/rfc/rfc2445.txt

    // iCal date format: yyyymmddThhiissZ
    // PHP equiv format: Ymd\This
    // 1. Set the corrent headers for this file
    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=' . $filename);

    // The Function
    // 2. Define helper functions

    function dateToCal($time) {
    return date('Ymd\This', $time) . 'Z';
    // Converts a unix timestamp to an ics-friendly format
    //
    // Returns an ics-formatted timestamp
    function dateToCal($timestamp) {
    return date('Ymd\This\Z', $timestamp);
    }

    // Build the ics file
    $ical = 'BEGIN:VCALENDAR
    // Escapes a string of characters
    //
    // Returns the escaped string
    function escapeString($string) {
    return preg_replace('/([\,;])/','\\\$1', $string);
    }

    // 3. Echo out the ics file's contents
    echo 'BEGIN:VCALENDAR
    VERSION:2.0
    PRODID:-//hacksw/handcal//NONSGML v1.0//EN
    CALSCALE:GREGORIAN
    BEGIN:VEVENT
    DTEND:' . dateToCal($event['dateend']) . '
    UID:' . md5($event['title']) . '
    DTSTAMP:' . time() . '
    LOCATION:' . addslashes($event['address']) . '
    DESCRIPTION:' . addslashes($event['description']) . '
    URL;VALUE=URI:http://mohawkaustin.com/events/' . $event['id'] . '
    SUMMARY:' . addslashes($event['title']) . '
    DTSTART:' . dateToCal($event['datestart']) . '
    DTEND:' . dateToCal($dateend) . '
    UID:' . uniqid() . '
    DTSTAMP:' . dateToCal(time()) . '
    LOCATION:' . escapeString($address) . '
    DESCRIPTION:' . escapeString($description) . '
    URL;VALUE=URI: ' . escapeString($uri) . '
    SUMMARY:' . escapeString($summary) . '
    DTSTART:' . dateToCal($datestart) . '
    END:VEVENT
    END:VCALENDAR';

    //set correct content-type-header
    if($event['id']){
    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=mohawk-event.ics');
    echo $ical;
    } else {
    // If $id isn't set, then kick the user back to home. Do not pass go, and do not collect $200.
    header('Location: /');
    }
    ?>
  12. Jake Bellacera revised this gist Apr 28, 2011. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    $event = array(
    'id' => $_GET['id'],
    'title' => $_GET['title'],
    'address' => $_GET['address'],
    'description' => $_GET['description'],
    'datestart' => $_GET['datestart'],
    'dateend' => $_GET['dateend'],
    @@ -27,7 +28,7 @@ function dateToCal($time) {
    DTEND:' . dateToCal($event['dateend']) . '
    UID:' . md5($event['title']) . '
    DTSTAMP:' . time() . '
    LOCATION:' . addslashes('912 Red River St, Austin, Texas') . '
    LOCATION:' . addslashes($event['address']) . '
    DESCRIPTION:' . addslashes($event['description']) . '
    URL;VALUE=URI:http://mohawkaustin.com/events/' . $event['id'] . '
    SUMMARY:' . addslashes($event['title']) . '
  13. Jake Bellacera revised this gist Apr 28, 2011. 1 changed file with 7 additions and 12 deletions.
    19 changes: 7 additions & 12 deletions PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    <?php
    // I know there are probably better ways to do this, but this accomplishes what I needed it to do.

    // Fetch vars. In this case, they're being pulled via the URL.
    // Fetch vars
    $event = array(
    'id' => $_GET['id'],
    'title' => $_GET['title'],
    @@ -11,13 +9,11 @@
    'address' => $_GET['stage']
    );

    // Convert times to iCalendar format. They require a block for yyyymmdd and then another block
    // for the time, which is in hhiiss. Both of those blocks are separated by a "T". The Z is
    // declared at the end for UTC time, but shouldn't be included in the date conversion.

    // iCal date format: yyyymmddThhiissZ
    // PHP equiv format: Ymd\This

    // The Function

    function dateToCal($time) {
    return date('Ymd\This', $time) . 'Z';
    }
    @@ -31,9 +27,9 @@ function dateToCal($time) {
    DTEND:' . dateToCal($event['dateend']) . '
    UID:' . md5($event['title']) . '
    DTSTAMP:' . time() . '
    LOCATION:' . addslashes('123 Fake St, MyCity, State 12345') . '
    LOCATION:' . addslashes('912 Red River St, Austin, Texas') . '
    DESCRIPTION:' . addslashes($event['description']) . '
    URL;VALUE=URI: http://mydomain.com/events/' . $event['id'] . '
    URL;VALUE=URI:http://mohawkaustin.com/events/' . $event['id'] . '
    SUMMARY:' . addslashes($event['title']) . '
    DTSTART:' . dateToCal($event['datestart']) . '
    END:VEVENT
    @@ -42,11 +38,10 @@ function dateToCal($time) {
    //set correct content-type-header
    if($event['id']){
    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=mydomain-event.ics');
    header('Content-Disposition: attachment; filename=mohawk-event.ics');
    echo $ical;
    } else {
    // If $id isn't set, then kick the user back to home. Do not pass go,
    // and do not collect $200. Currently it's _very_ slow.
    // If $id isn't set, then kick the user back to home. Do not pass go, and do not collect $200.
    header('Location: /');
    }
    ?>
  14. jakebellacera revised this gist Oct 19, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ function dateToCal($time) {
    //set correct content-type-header
    if($event['id']){
    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=mohawk-event.ics');
    header('Content-Disposition: attachment; filename=mydomain-event.ics');
    echo $ical;
    } else {
    // If $id isn't set, then kick the user back to home. Do not pass go,
  15. jakebellacera created this gist Oct 19, 2010.
    52 changes: 52 additions & 0 deletions PHPtoICS.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?php
    // I know there are probably better ways to do this, but this accomplishes what I needed it to do.

    // Fetch vars. In this case, they're being pulled via the URL.
    $event = array(
    'id' => $_GET['id'],
    'title' => $_GET['title'],
    'description' => $_GET['description'],
    'datestart' => $_GET['datestart'],
    'dateend' => $_GET['dateend'],
    'address' => $_GET['stage']
    );

    // Convert times to iCalendar format. They require a block for yyyymmdd and then another block
    // for the time, which is in hhiiss. Both of those blocks are separated by a "T". The Z is
    // declared at the end for UTC time, but shouldn't be included in the date conversion.

    // iCal date format: yyyymmddThhiissZ
    // PHP equiv format: Ymd\This

    function dateToCal($time) {
    return date('Ymd\This', $time) . 'Z';
    }

    // Build the ics file
    $ical = 'BEGIN:VCALENDAR
    VERSION:2.0
    PRODID:-//hacksw/handcal//NONSGML v1.0//EN
    CALSCALE:GREGORIAN
    BEGIN:VEVENT
    DTEND:' . dateToCal($event['dateend']) . '
    UID:' . md5($event['title']) . '
    DTSTAMP:' . time() . '
    LOCATION:' . addslashes('123 Fake St, MyCity, State 12345') . '
    DESCRIPTION:' . addslashes($event['description']) . '
    URL;VALUE=URI: http://mydomain.com/events/' . $event['id'] . '
    SUMMARY:' . addslashes($event['title']) . '
    DTSTART:' . dateToCal($event['datestart']) . '
    END:VEVENT
    END:VCALENDAR';

    //set correct content-type-header
    if($event['id']){
    header('Content-type: text/calendar; charset=utf-8');
    header('Content-Disposition: attachment; filename=mohawk-event.ics');
    echo $ical;
    } else {
    // If $id isn't set, then kick the user back to home. Do not pass go,
    // and do not collect $200. Currently it's _very_ slow.
    header('Location: /');
    }
    ?>