Skip to content
Advertisement

Subscription Calendar URL in PHP

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?

Advertisement

Answer

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('YmdThis', 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;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement