【问题标题】:Wrong time in a ICS fileICS 文件中的错误时间
【发布时间】:2015-11-30 13:46:14
【问题描述】:

我正在开发一个 PHP 工具来创建一个将通过邮件发送的 ICS 文件。

创建文件后,我尝试将其添加到 Outlook 2016 或 iCalendar (Apple) 中。除开始时间和结束时间外,所有信息均正确无误。它们偏离一小时。

示例:

BEGIN:VCALENDAR
METHOD:PUBLISH
VERSION:2.0
PRODID:-//Communication Maker
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTART;TZID=Europe/Zurich:20151201T150000Z
DTEND;TZID=Europe/Zurich:20151201T180000Z
UID:565c50b5ca7d9
LOCATION:Location
SUMMARY:Title
DESCRIPTION:Content
END:VEVENT
END:VCALENDAR

这是文件信息:

开始时间:01/12/2015 @ 15:00:00

结束时间:01/12/2015 @ 18:00:00

时区:UTC +01:00(欧洲/苏黎世)

这里是 Outlook 和 iCalendar 上的结果:

开始时间:01/12/2015 @ 16:00:00

结束时间:01/12/2015 @ 19:00:00

我已经搜索了 4 天,但找不到使用正确数据进行活动的答案。

如果你愿意,我可以给你更多我的代码(HTML 或 PHP 类)。

有我的课:

class ICS {

    private $sSaveDir        = './icsFiles/';
    private $sIcsContent     = '';
    private $sIcsDateFormat  = 'Ymd\THis\Z';

    public function __construct($sTitle = null, $sLocation = null, $sUrl = null, $sTimezoneValue = null, $sEventText = null, $sDateS = null, $sTimeS = null, $sDateE = null, $sTimeE = null) {

        // Timezone par défaut
        date_default_timezone_set('UTC');

        // Génération de l'ID unique
        $sUniqId = uniqid();

        // Construction du array
        $aIcsContent = array(
            "BEGIN:VCALENDAR",
            "METHOD:PUBLISH",
            "VERSION:2.0",
            "PRODID:-//Communication Maker",
            "CALSCALE:GREGORIAN",
            "BEGIN:VEVENT",
            "DTSTART;TZID=".$sTimezoneValue.":".date($this->sIcsDateFormat, strtotime($sDateS." ".$sTimeS)),
            "DTEND;TZID=".$sTimezoneValue.":".date($this->sIcsDateFormat, strtotime($sDateE." ".$sTimeE)),
            "UID:".$sUniqId,
            "LOCATION:".$sLocation,
            "SUMMARY:".$sTitle,
            "DESCRIPTION:".$sEventText,
            "END:VEVENT",
            "END:VCALENDAR"
        );

        // Array => string
        $this->sIcsContent = implode(PHP_EOL, $aIcsContent);

        // Créer et ouvre le fichier en écriture seule
        if($oIcsFile = fopen($this->sSaveDir.'event_'.$sUniqId.'.ics', 'w')) {

            // Inscrit les données de l'événement dans le fichier
            fwrite($oIcsFile, $this->sIcsContent);

            // Ferme le fichier proprement
            fclose($oIcsFile);

            echo 'true';
        }
        else {

            echo 'false';
        }

    }

}

感谢您的帮助。我真的需要它。

【问题讨论】:

    标签: php outlook icalendar


    【解决方案1】:

    您的 ICS 文件显示时间是 01/12/2015 @ 15:00:00 UTC 时区。它说以苏黎世时间显示时间。苏黎世是 UTC+100,因此将时间显示为 01/12/2015 @ 16:00:00 苏黎世时间(中欧时间)是正确的。

    20151201T150000Z 末尾的 Z 表示“祖鲁时间”,它(大致)是 UTC 时间的另一个名称。

    要以苏黎世时间而不是 UTC 指定活动开始的日期/时间,只需从时间中删除 Z,如下所示:20151201T150000

    如果您希望坐在 UTC+100 的人看到 15:00,而坐在 UTC+200 的人看到 16:00 作为时间,您应该指定 UTC 时间。在这种情况下,您可以将时间设置为 20151201T140000Z,因为这相当于 2015 年 1 月 12 日 15:00:00 UTC+100 的 UTC。

    【讨论】:

    • 是的,但我想为 UTC+01:00 显示 15:00。如果您使用 UTC+02:00 的日历打开日历,您将看到 16:00。
    • 你是说你想让 UTC+200 的人看到 16:00,而 UTC+100 的人看到 15:00?
    • 是的,这正是我想要的。
    • 如果我用时区转换日期怎么办?类似的东西:date($this->sIcsDateFormat, strtotime($sDateS." ".$sTimeS." ".$sTimezoneValue))
    • 我建议 stackoverflow.com/questions/10711597/… 将时间转换为 UTC。
    猜你喜欢
    • 2018-12-10
    • 2012-05-20
    • 2014-08-16
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多