Skip to content

Instantly share code, notes, and snippets.

@tom-henderson
Created August 23, 2016 21:46
Show Gist options
  • Save tom-henderson/c1f03c5dbbd986b6e9df72ff544aee43 to your computer and use it in GitHub Desktop.
Save tom-henderson/c1f03c5dbbd986b6e9df72ff544aee43 to your computer and use it in GitHub Desktop.
<?php
# Read an RSS feed and generate an ical calendar with the feed items as events.
#
# Written by Tom Henderson, 31 May 2007
#
# Use however you like, but please attribute my code to me.
# If you come up with anything cool I'd love to hear about it.
#
# Usage:
# http://path/to/rss-ics.php?url=http://path/to/feed/
# MagpieRSS files
require_once('../tools/magpierss-0.72/rss_fetch.inc');
require_once('../tools/magpierss-0.72/rss_utils.inc');
# Calendar properties
$xwrcalname = "RSScal"; # Default name for the calendar
$prodid = "-//Generated by RSScal//Tom Henderson 2007";
$duration = 60 * 60 * 3; # Duration for the events (10,800 seconds == 3 hours)
# Querystring options
$url = $_GET['url']; # Feed URL
$rss = fetch_rss($url);
if ( $rss ) {
# Debug
#print_r($rss);
# Update the calendar name from the feed data
$xwrcalname = $rss->channel['title'];
$events = "";
# Loop through the feed items and format an event for each
foreach ( $rss->items as $item ) {
$title = $item['title'];
$description = $item['description'];
$description = ""; # Ignore description for now.
if ( $item['pubdate'] == '' ) {
# Get the date using the dc:date field
# Handy code by Chrys Bader: http://www.php.net/manual/en/function.strtotime.php#74495
$rfc3339 = $item['dc']['date']; # Date is in RFC3339 format
$tmp = str_replace( "T", " ", $rfc3339 ); # Remove the T
$tmp = ereg_replace( "(\.[0-9]{1,})?", "", $tmp ); # Remove the decimal and any numbers after it
$datetime = substr($tmp, 0, 19); # Grab the datetime
$timezone = str_replace( ":", "", substr($tmp, 19, 6) ); # Grab the timezone
$pubdate = strtotime( $datetime . " " . $timezone );
# Thanks Chris
} else {
# Get the date with the pubdate field
$pubdate = strtotime( $item['pubdate'] );
}
$dtstart = date( 'Ymd\THis', $pubdate );
$dtend = date( 'Ymd\THis', ( $pubdate + $duration ) );
$url = $item[link];
$events .= "BEGIN:VEVENT\n";
$events .= "URL;VALUE=URI:$url\n";
$events .= "DTSTART:$dtstart\n";
$events .= "DTEND:$dtend\n";
$events .= "SUMMARY:$title\n";
$events .= "DESCRIPTION:$description\n";
$events .= "END:VEVENT\n";
}
} else {
# The calendar must have 1 event
# Make one for now containing the error message
$pubdate = strtotime( "Now" );
$today = date( 'Ymd', $pubdate );
$events = "BEGIN:VEVENT\n";
$events .= "URL;VALUE=URI:http://www.blogtender.com/\n";
$events .= "DTSTART:" . $today . "T120000\n";
$events .= "DTEND:" . $today . "T163000\n";
$events .= "SUMMARY:" . magpie_error() . "\n";
$events .= "END:VEVENT\n";
}
?>
BEGIN:VCALENDAR
VERSION:2.0
X-WR-CALNAME:<?php echo $xwrcalname . "\n"; ?>
PRODID:<?php echo $prodid . "\n"; ?>
CALSCALE:GREGORIAN
METHOD:PUBLISH
<?php echo $events; ?>
END:VCALENDAR
@Jan02
Copy link

Jan02 commented Aug 23, 2016

Could it be updated to work with future versions of php ?

@tom-henderson
Copy link
Author

Probably. I've not used this code in about 9 years though! What's not working?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment