Question

Hi, I am working on a project based on event management where each user store there event detail with start and end date. Now I want a feature to subscribe to the desktop application (Outlook, Ical for apple and google calendar), so that any new event save in the database it automatically sync to the desktop application. What is the best approach to achieve this functionality?

Was it helpful?

Solution 2

Here's sample code for generating a single event iCal output:

$eventData = array(
        'title'       => $event->getTitle(),
        'address'     => $address,
        'description' => strip_tags($event->getBody()),
        'stage'       => $stage,
        'date'        => $event->getDate()
    );

    // Build the ics file


    $ical= 'BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTEND:' . $this->dateToCal($eventData['date']) . '
UID:' . md5($eventData['title']) . '
DTSTAMP:' . time() . '
LOCATION:' . $eventData['address'] . '
DESCRIPTION:' . $eventData['description'] . '
URL;VALUE=URI:http://go.okdo.it' . '
SUMMARY:' . $eventData['title'] . '
DTSTART:' . $this->dateToCal($eventData['date']) . '
END:VEVENT
END:VCALENDAR';

Here's the function that formats date objects to iCal format:

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

Before outputting the content, you need to set appropriate headers:

   header('Content-type: text/calendar; charset=utf-8');
   header('Content-Disposition: attachment; filename=' . $task->getTitle());
   echo $ical;

OTHER TIPS

I started with @masnun's answer and here's what I've got.

once you have this file outputting a valid ical feed (there are online validators to check), you can access this file in your browser via webcal://yoursite.com/your-ical-script.php and it will give you a subscribe option

$ical = "BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN";

foreach($class_list_temp as &$c){
    $c["description"] = strip_tags($c["blurb"] . " " . $c["url"]);
    $c["dtstart"] = strtotime($c["date"]);
$c["dtend"] = (int)($c["dtstart"]) + 60*60; // hour duration
    $ical .= "\nBEGIN:VEVENT
UID:" . md5($c["title"] . $c["id"] . $c["dtstart"]) . "
DTSTAMP:" . dateToCal(time()) . "
" . wordwrap("DESCRIPTION:" . $c["description"], 75, "\n") . "
URL;VALUE=URI:" . $c["url"] . "
SUMMARY:" . $c["title"] . "
DTSTART:" . dateToCal($c["dtstart"]) . "
DTEND:" . dateToCal($c["dtend"]) . "
END:VEVENT";
}

$ical .= "\nEND:VCALENDAR";
$ical = str_replace(["\r\n", "\r", "\n"], "\r\n", $ical); //fix linebreaks

function dateToCal($timestamp){
    return gmdate("Ymd",$timestamp)."T". gmdate("His",$timestamp) . "Z";
}
header("Content-type: text/calendar; charset=utf-8");
header("Content-Disposition: attachment; filename=calendar.ics");
echo $ical;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top