I have seen various ways to parse webcal (ical, ics) files in PHP but they tend to get a little more complicated than they need to in some cases or simply do not work.
<?phpPHP
$icalURL = '<facebook URL>';
echo '<p>iCal URL:<br/>' . $icalURL . '</p>';
$ical = file_get_contents($icalURL);
$sED_Start = '';
$sED_End = '';
$sED_Title = '';
$sED_Location = '';
$sED_URL = '';
$sED_Description = '';
$sLineBreak = array(
"rn",
"n",
"r"
);
$sLineBreakReplace = '<br />';
//echo '<p>iCal Raw:<blockquote>' . str_replace($sLineBreak, $sLineBreakReplace, $ical) . '</blockquote></p>';
$event = '';
$tmpbyline = explode("rn", $ical);
foreach ($tmpbyline as $item) {
//echo $item . '<br/>'; // Display it ical line by line (almost useless)
$tmpholderarray = explode(":", $item);
switch ($tmpholderarray[0]) {
case 'DTSTART':
$sED_Start = dtCleanDateTime($tmpholderarray[1]);
break;
case 'DTEND':
$sED_End = dtCleanDateTime($tmpholderarray[1]);
break;
case 'SUMMARY':
$sED_Title = $tmpholderarray[1];
break;
case 'LOCATION':
$sED_Location = $tmpholderarray[1];
break;
case 'URL':
$sED_URL = $tmpholderarray[1];
break;
case 'DESCRIPTION':
$sED_Description = $tmpholderarray[1];
break;
}
if ($item == 'END:VEVENT') {
$event = $event . $sED_Start . '-' . $sED_End . ' ' . $sED_Title . '<br/>';
}
}
function dtCleanDateTime($date)
{
$date = str_replace('T', '', $date); //remove T
$date = str_replace('Z', '', $date); //remove Z
$d = date('d', strtotime($date)); //get date day
$m = date('m', strtotime($date)); //get date month
$y = date('Y', strtotime($date)); //get date year
$now = date('Y-m-d G:i:s'); //current date and time
$eventdate = date('Y-m-d G:i:s', strtotime($date)); //user friendly date
return $eventdate;
}
echo $event;
?>
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.