我正在尝试在PHP中动态生成ics文件,其中时区根据给定的位置是动态的。一切都很好但有日光时间问题,即它显示一小时左右的时差。现在要解决这个问题,我必须动态地使用DAYLIGHT
。但我不知道如何动态使用它,或者我可以从哪里获得与给定时区相关的TZOFFSETFROM
和TZOFFSETTO
偏移量。
例如:
$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;
现在如何根据位置动态使用日光,如位置可以是'America / Caracas'等等
$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";
提前致谢。
答案 0 :(得分:1)
实际上,我有1天的活动持续时间,这就是我做的,我认为它对我来说工作正常,但我不是100%肯定,所以如果我出错了,请帮助我。感谢@ Ph.T和@Matt Johnson的支持。
$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);
编辑:
通过Ph.T:你不应该使用time()但是$ start_time和$ end_time一致且更正确。但是它没有任何区别,因为如果不使用夏令时,起始区域与现在,明天或一百万年相同。您可能遇到的唯一差距是,在“现在”和“开始日期”之间有效地存在不同的夏令时,例如您将下周的事件设置为在本周结束时更改时间。并非所有国家都使用夏令时。
$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"
因此
$ical .= "BEGIN:VTIMEZONE\n";
$ical .= "TZID:{$timeZone}\n";
$ical .= "TZURL:http://tzurl.org/zoneinfo-outlook/{$timeZone}\n";
$ical .= "X-LIC-LOCATION:{$timeZone}\n";
$ical .= "BEGIN:DAYLIGHT\n";
$ical .= "TZOFFSETFROM:{$start_zone}\n";
$ical .= "TZOFFSETTO:{$end_zone}\n";
$ical .= "TZNAME:". date("T")."\n";
$ical .= "DTSTART:{$start_date}\n";
$ical .= "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU\n";
$ical .= "END:DAYLIGHT\n";
$ical .= "BEGIN:STANDARD\n";
$ical .= "TZOFFSETFROM:{$start_zone}\n";
$ical .= "TZOFFSETTO:{$end_zone}\n";
$ical .= "TZNAME:".date("T")."\n";
$ical .= "DTSTART:{$start_date}\n";
$ical .= "RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU\n";
$ical .= "END:STANDARD\n";
$ical .= "END:VTIMEZONE\n";
答案 1 :(得分:0)
在转换时间和日期之前,您应该在PHP中设置正确的时区,以便引擎知道您正在使用的区域的时间特征:
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"
希望这可能会有所帮助