Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PHP Script to Add Time Zones to iCalendar/vCal Feeds
$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" .
"TZID:America/Los_Angeles\n" .
"TZOFFSETFROM:-0800\n" .
"DTSTART:20070311T020000\n" .
"TZOFFSETTO:-0700\n" .
"TZOFFSETFROM:-0700\n" .
"DTSTART:20071104T020000\n" .
"TZOFFSETTO:-0800\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;
<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="">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>
$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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.