【问题标题】:ISO Datetime in PHPPHP 中的 ISO 日期时间
【发布时间】:2014-02-18 07:36:10
【问题描述】:

我有一个从网络日历 ICS 文件中解析出来的 ISO DATETIME 格式。我在 google 和 yahoo 中创建了日历事件。在谷歌中,我创建了 IST 11:00 到 11:30 的时间。 在雅虎,我在 IST 11:30 到 11:45 的时间创建。

我已收到这两个事件的 ICS 文件。我的问题是 ICS 文件中的事件开始时间和结束时间。

Google 开始时间 - 20140218T060000Z(提供 GMT 时间)

Yahoo 开始时间 - 20140218T113000Z(提供当地时间)

如何检查 ISO 日期时间中的时区是否为 GMT,如果不是,则将其转换为 GMT?请帮助我实现这一目标。提前致谢。

【问题讨论】:

  • 这是不可能的,因为根据它们的字符串表示,两个时区都是 Z(ulu) aka GMT。
  • @Jack SO 提到这些日期已被解析。

标签: php datetime timezone icalendar rfc5545


【解决方案1】:

您可以使用的 ical 文件中应该有一个时区声明。

The RFC 将此放在DTSTART 上,例如:

DTSTART;TZID=America/New_York:19980119T020000

但 ical 并不是一门非常精确的“科学”,似乎每个人使用它的方式都不一样。我见过这样的时区声明:

X-WR-TIMEZONE:Europe/Berlin

在 ical 文件中只出现一次。

创建 DateTime 对象后,您可以使用 DateTime::setTimezone 来设置它。来自PHP documentation 的示例:

$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";

【讨论】:

    【解决方案2】:

    正如 Gerald 所指出的,区别在于事件的 DTSTART(顺便说一下,还有 DTEND)属性被显式引用(根据 RFC 5545 - time zone identifier)或通过 X-WR-TIMEZONE 隐式引用(苹果 iCal)。

    在第一种情况下,您必须处理 VTIMEZONE 组件 (RFC5545 - VTIMEZONE component)。为此您需要 ICalendar parser in PHP that supports timezones

    在第二种情况下,您必须引用Olson database,并且可能需要使用timezonedb 而不是默认的php。

    但是,在开始这段旅程之前,您可能应该阅读 J. Dunnell 的这篇 blog 条目,了解与 X-WR-TIMEZONE 打交道的可能陷阱......

    【讨论】:

      【解决方案3】:

      你可以在php中使用datestrtotime函数。

        // apply this to all your times
        $start_time = "20140218T060000Z";
        $timeZone = date("e",strtotime($start_time));
      
        if($timeZone != "UTC"){ // UTC is same as GMT in case you're wondering
            $start_time_gmt = gmdate("Ymd\THis\Z",$start_time); // the \T an \Z are key characters for the date function so you need to escape them if you want to display it in that format
        }
      

      参考资料: http://www.php.net/manual/en/function.date.php

      http://php.net/strtotime

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        相关资源
        最近更新 更多