dtcwehta624485
dtcwehta624485
2013-05-14 13:18

ICS文件动态时区问题[重复]

已采纳

This question already has an answer here:

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

Thanks in advance.

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duande8497 duande8497 8年前

    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

    点赞 评论 复制链接分享
  • duangong0690 duangong0690 8年前

    Actually, I have 1 day event duration and here is what I did, I think it is working fine for me but I am not 100% sure, so please help me if I am somewhere wrong. Thanks to @Ph.T and @Matt Johnson for support.

      $timeZone = "America/Denver" // dynamically fetched from DB
    
      $date = date('Y-m-d', strtotime($event_date));
    
      $start_time = date('H:i:s', strtotime($event_start_time));
      $start_timestamp = strtotime(date($date .' '.$start_time));
    
      $end_time = date('H:i:s', strtotime($event_end_time));
      $end_timestamp = strtotime(date($date .' '.$end_time));       
    
      $start_date = date('Ymd', strtotime($event_date)) ."T". date('His', $start_timestamp); 
      $end_date = date('Ymd', strtotime($event_date)) ."T". date('His', $end_timestamp); 
    

    EDIT:
    By Ph.T: You should not use time() but $start_time and $end_time to be consistent and more correct. However it does not make any difference since the start zone is the same as now, tomorrow or in one million years if you dont use daylight savings. The only gap you could have is when there is efectively a different daylight saving between "now" and "the start date" , like for example you set the event for next week while the time will be changed this week-end. Not all the countries use the daylight savings.

      $start_zone = date('O', strtotime($event_start_time));   // TZOFFSETFROM format  of "right now"  
      date_default_timezone_set($timeZone);  
      $end_zone = date('O', strtotime($event_end_time);  // TZOFFSETTO of "right now"
    

    And therefore

      $ical .= "BEGIN:VTIMEZONE
    ";
      $ical .= "TZID:{$timeZone}
    ";
      $ical .= "TZURL:http://tzurl.org/zoneinfo-outlook/{$timeZone}
    ";
      $ical .= "X-LIC-LOCATION:{$timeZone}
    ";
      $ical .= "BEGIN:DAYLIGHT
    ";
      $ical .= "TZOFFSETFROM:{$start_zone}
    ";
      $ical .= "TZOFFSETTO:{$end_zone}
    ";
      $ical .= "TZNAME:". date("T")."
    ";
      $ical .= "DTSTART:{$start_date}
    ";
      $ical .= "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
    ";
      $ical .= "END:DAYLIGHT
    ";
      $ical .= "BEGIN:STANDARD
    ";
      $ical .= "TZOFFSETFROM:{$start_zone}
    ";
      $ical .= "TZOFFSETTO:{$end_zone}
    ";
      $ical .= "TZNAME:".date("T")."
    ";
      $ical .= "DTSTART:{$start_date}
    ";
      $ical .= "RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
    ";
      $ical .= "END:STANDARD
    ";      
      $ical .= "END:VTIMEZONE
    ";
    
    点赞 评论 复制链接分享

为你推荐