#native_company# #native_desc#
#native_cta#

Using PHP to Make Basic vCalendar/iCalendar Events Page 4

By PHP Builder Staff
on October 30, 2002

Enter PHP

Now that we know how to format a vCalendar file, we can use PHP to format the data and prepare the file. As an example, suppose we store events in a database table with the following fields:
EventID, Summary, Detail, EventStart, EventEnd
In the link to the vcs generation script, pass a url variable that holds the event ID. Use this variable to specify the exact event in my SQL statement. It’s also a good idea to use this ID to generate a unique filename for the vcs file. This is necessary because if a user clicks on more than one event and downloads more than one vcs file, the operating system may append a number after the ..vcs extension. This number disrupts the browser’s handling of the vcs file. So, the first few lines in our script are:

<?php

    $Filename 
"SSPCAEvent" $_GET['ID'] . ".vcs"

    
header("Content-Type: text/x-vCalendar");

    
header("Content-Disposition: inline; filename=$Filename");

?>



After this, hit the database to get the stored event data. I’ll use a mysql/mysql_fetch_array example. Use the results to populate a correct vCalendar file. We’ll use the Summary result in the SUMMARY element of the vCalendar file and the Detail result in the DESCRIPTION element. Feel free to add any other stored details in the DESCRIPTION element. For example, in a community calendar I support, I append a contact’s name and phone number at the end of each description. Before you populate the DESCRIPTION element, remember to substitute line breaks for the appropriate CFLF hex values.

<?php

    $DescDump 
str_replace("r""=0D=0A="$row['Detail']);

?>



Now, parse the start and end dates and times to the vCalendar format. Let’s assume that the start and end information are stored as unix timestamp values. PHP’s date function makes it easy to break out the date and time parts from unix timestamps, so we’ll use it. If your information is not in unix timestamp values, you’ll need to use another method to parse the date and time information to their year, month, day, hour, minute, and seconds. Don’t forget to add leading zeros to any single digit units.
If multiple time zones are a concern, convert the start and end times to UTC before you parse the timestamp. For example, if the server is in the U.S. Pacific time zone, add 25,200 (number of seconds in seven hours, daylight savings time) to the unix timestamp. Remember, you’ll also need to add the Z flag at the end of the string. For now, we’ll keep things simple and ignore UTC.

<?php

$vCalStart 
date("YmdTHi00"$row['EventStart']);

$vCalEnd date("YmdTHi00"$row['EventEnd']);

?>



We’ve now grabbed and formatted the event’s summary, description, start time, and end times. The final step is to take this data and make the vCalendar file:

<?php

    $Filename 
"SSPCAEvent" $_GET['ID'] . ".vcs"

    
header("Content-Type: text/x-vCalendar");

    
header("Content-Disposition: inline; filename=$Filename");

    /** Put mysql connection and query statements here **/

$DescDump str_replace("r""=0D=0A="$row['Detail']);

$vCalStart date("YmdTHi00"$row['EventStart']);

$vCalEnd date("YmdTHi00"$row['EventEnd']);

?>

BEGIN:VCALENDAR

VERSION:1.0

PRODID:SSPCA Web Calendar

TZ:-07

BEGIN:VEVENT

SUMMARY:<?php echo $row['Summary'] . "n"?>

DESCRIPTION;ENCODING=QUOTED-PRINTABLE: <?php echo $DescDump "n"?>

DTSTART:<?php echo $vCalStart "n"?>

DTEND:<?php echo $vCalEnd "n"?>

END:VEVENT

END:VCALENDAR