Question

I am trying to generate ics file dynamically in PHP, where timezone is dynamic according to the location given. Everything works good but there is daylight time issue i.e it shows time difference of one hour or so. Now to solve this issue I have to use DAYLIGHT dynamically. but I don't know how to use it dynamically, or where from can I get TZOFFSETFROM and TZOFFSETTO offsets related with timezone given.

For Example:

    $timeZone = "America/Denver" // dynamically fetched from DB

      $ical = "BEGIN:VCALENDAR\n";
      $ical .= "VERSION:2.0\n";
      $ical .= "PRODID:-//LokalMotion//LokalMotion Events v1.0//EN\n";
      $ical .= "CALSCALE:GREGORIAN\n";
      $ical .= "METHOD:PUBLISH\n";
      $ical .= "X-WR-CALNAME:LokalMotion Events\n";
      $ical .= "X-MS-OLK-FORCEINSPECTOROPEN:TRUE\n";
      $ical .= "BEGIN:VTIMEZONE\n";
      $ical .= "TZID:{$timeZone}\n";
      $ical .= "TZURL:http://tzurl.org/zoneinfo-outlook/{$timeZone}\n";
      $ical .= "X-LIC-LOCATION:{$timeZone}\n";
      $ical .= "END:VTIMEZONE\n";
      $ical .= "BEGIN:VEVENT\n";
      $ical .= "DTSTAMP:".date('Ymd\THis\Z')."\n";
      $ical .= "DTSTART;TZID={$timeZone}:{$start_date}\n";
      $ical .= "DTEND;TZID={$timeZone}:{$end_date}\n";
      $ical .= "STATUS:CONFIRMED\n";
      $ical .= "SUMMARY:{$title}\n";
      $ical .= "DESCRIPTION:{$description}\n";
      $ical .= "ORGANIZER;CN=Reminder:MAILTO:support@mysite.com\n";
      $ical .= "CLASS:PUBLIC\n";
      $ical .= "CREATED:{$start_date}Z\n";
      $ical .= "LOCATION:{$location}\n";
      $ical .= "URL:http://www.mysite.com\n";
      $ical .= "SEQUENCE:1\n";
      $ical .= "LAST-MODIFIED:".date('Ymd\THis\Z')."\n";
      $ical .= "UID:{$title}-support@mysite.com\n";
      $ical .= "END:VEVENT\n";
      $ical .= "END:VCALENDAR";    

echo $ical;

Now how to use daylight dynamically according to the location, like location can be 'America/Caracas' .. etc

$ical .= "BEGIN:DAYLIGHT";
$ical .= "TZOFFSETFROM:{}"; //I need this dynamic
$ical .= "TZOFFSETTO:{}";//I need this dynamic
$ical .= "TZNAME:EDT";
$ical .= "DTSTART;TZID={$timeZone}:{$start_date}\n";
$ical .= "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU";
$ical .= "END:DAYLIGHT";

Thanks in advance.

Was it helpful?

Solution

Before converting times and date, you should set the correct timezone in PHP so the engine knows the time characteristics of the zone you are using:

date_default_timezone_set('America/Mexico_City');
$start_date = date('c', time());   // ISO date 8601 of "right now"
$start_zone = date('O', time());   // TZOFFSETFROM format  of "right now"

date_default_timezone_set('America/Denver');
$to_zone = date('O', time());   // TZOFFSETTO of "right now"

Hope this may help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top