Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
PHP Script to Add Time Zones to iCalendar/vCal Feeds
<?php
$filename = "calendar";
if (isset($_GET["url"])) {
$url = $_GET["url"];
if (!isset($_GET["show_url"])) {
preg_match("|.+\/([^?]+)\??|", $url, $matches);
if (isset ($matches[1])) {
$filename = $matches[1];
}
$calendar = file_get_contents ($url);
if ($calendar) {
//$output = preg_replace_callback ("/(DATE-TIME:)(\d{4,4})(\d{2,2})(\d{2,2})T(\d{2,2})(\d{2,2})(\d{2,2})[^Z]/", "adjustTimeZone", $calendar);
$timezone = "X-WR-TIMEZONE:America/Los_Angeles\n" .
"CALSCALE:GREGORIAN\n" .
"BEGIN:VTIMEZONE\n" .
"TZID:America/Los_Angeles\n" .
"BEGIN:DAYLIGHT\n" .
"TZOFFSETFROM:-0800\n" .
"RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU\n" .
"DTSTART:20070311T020000\n" .
"TZNAME:PDT\n" .
"TZOFFSETTO:-0700\n" .
"END:DAYLIGHT\n" .
"BEGIN:STANDARD\n" .
"TZOFFSETFROM:-0700\n" .
"RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU\n" .
"DTSTART:20071104T020000\n" .
"TZNAME:PST\n" .
"TZOFFSETTO:-0800\n" .
"END:STANDARD\n" .
"END:VTIMEZONE\n";
$loc = strpos($calendar, "BEGIN:VEVENT");
$output = substr($calendar, 0, $loc) . $timezone . substr($calendar, $loc, strlen($calendar));
header("Content-Type: text/calendar");
header("Content-Disposition: inline; filename=$filename-pacific-timezone.ics");
echo $output;
exit;
}
}
}
?>
<h1>vCalendar Time Zone timezone</h1>
<p>This is quick script to "de-float" calendars in the vCalendar format which do not specify time zones for their events. This script will automatically add the Pacific time zone information to the calendar at the URL entered below. Copy-and-paste the resulting URL below into your calendar reader of choice. <a href="http://battis.net/link/timezonescript">The source of this script is freely available.</a></p>
<form action="<?= $_SERVER["PHP_SELF"] ?>" method="get">
<input type="hidden" name="show_url" value="" />
<p>Calendar URL <input name="url" type="text" value="<?= $url ?/>" /></p>
<?php
$newUrl = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"] . "?url=" . urlencode($url);
echo "<p><a href=\"$newUrl\">$newUrl</a>";
?>
<p><input type="submit" value="Generate"/></p>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment