Here is a script that I created that generates a vcal file from a database.
for my party planner
I use a separate table to account for different time zones and dst.
<?
$str = "BEGIN:VCALENDAR";
$str .= "\r\nPRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN";
$str .= "\r\nVERSION:1.0";
$str .= "\r\nBEGIN:VEVENT";
$db = mysql_connect("localhost","login","password");
mysql_select_db (database);
$result = mysql_query ("select * from Parties where id = '$QpartyID'");
while ($row = mysql_fetch_array($result)) {
$rawDate = $row["date"];
$GMTabbrev = $row["GMToffset"];
$result2 = mysql_query ("select offset from time_zones where abbreviation = '$GMTabbrev'");
while ($row2 = mysql_fetch_array($result2))
{
$offset = $row2["offset"];
}
if($offset < 0){
$offset = str_replace (":3","7", $offset);
}else{
$offset = str_replace (":","", $offset);
}
$GMToffset = $offset;
$time = $row["time"];
$time = str_replace (":","", $time);
$time = str_replace (" ","", $time);
$time = str_replace (".","", $time);
if (preg_match ("/am/i", "$time")) {
$time = str_replace ("am","", $time);
if($time == 1200){
$time = $time - 1200;
}
}else{
$time = str_replace ("pm","", $time);
if($time != 1200){
$time = $time + 1200;
}
}
$endTime = $row["endTime"];
$endTime = str_replace (":","", $endTime);
$endTime = str_replace (" ","", $endTime);
$endTime = str_replace (".","", $endTime);
if (preg_match ("/am/i", "$endTime")) {
$endTime = str_replace ("am","", $endTime);
if($endTime == 1200){
$endTime = $endTime - 1200;
}
}else{
$endTime = str_replace ("pm","", $endTime);
if($endTime != 1200){
$endTime = $endTime + 1200;
}
}
$time = $time + $GMToffset;
$endTime = $endTime + $GMToffset;
if ($time < 0){$time = $time + 2400;}
if ($time >= 2400){$time = $time - 2400;}
if ($endTime < 0){$endTime = $endTime + 2400;}
if ($endTime >= 2400){$endTime = $endTime - 2400;}
if($time < 1000){$time = "0".$time;}
if($endTime < 1000){$endTime = "0".$endTime;}
if($time == 0){$time = "0000".$time;}
if($endTime == 0){$endTime = "0000".$endTime;}
if($endTime < $time){$nextDay = "t";}
//echo $offset;
//exit;
$str .= "\r\nDTSTART:".date("Ymd",strtotime($rawDate))."T".$time."00Z";
if($nextDay == "t"){
$str .= "\r\nDTEND:".date("Ymd",strtotime("$rawDate + 1 day"))."T".$endTime."00Z";
}else{
$str .= "\r\nDTEND:".date("Ymd",strtotime($rawDate))."T".$endTime."00Z";
}
$str .= "\r\nLOCATION;ENCODING=QUOTED-PRINTABLE:".$row["address"];
$str .= " ".$row["city"];
$str .= " ".$row["state"];
$str .= " ".$row["zip"];
$str .= "\r\nUID:040000008200E00074C5B7101A82E00800000000800289520E36C501000000000000000010000000693026094A702C4DA2130421B20DBF2A";
$str .= "\r\nDESCRIPTION;ENCODING=QUOTED-PRINTABLE: ".$row["description"];
$str .= " ".$row["organizer"];
$str .= " (".$row["organizerName"];
$str .= ") ".$row["phone"];
$str .= " ".$row["email"];
$str .= "\r\nSUMMARY;ENCODING=QUOTED-PRINTABLE:".$row["partyName"];
}
$str .= "\r\nPRIORITY:3";
$str .= "\r\nEND:VEVENT";
$str .= "\r\nEND:VCALENDAR";
$FilenameForDownload = "party.vcs";
$fpc = fopen($FilenameForDownload,'w');
fwrite($fpc,$str);
fclose($fpc);
$download_size = filesize($FilenameForDownload);
header("Content-Type: application/text");
header("Content-Disposition: attachment; filename=$FilenameForDownload;");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size");
@readfile($FilenameForDownload);
?>