gpt4 book ai didi

php - 使用 PHP 解析 Google Calendar XML - 结束日期相差一天

转载 作者:可可西里 更新时间:2023-10-31 23:33:57 26 4
gpt4 key购买 nike

我正在尝试制作一个网页,并在主页上显示 Google 日历中接下来的三个事件。我一直在使用这个 PHP (http://james.cridland.net/code/google-calendar.html) 来访问我的 XML 提要并将其格式化为 HTML。

我遇到的问题是,由于某种原因,新的一天从上午 11 点开始。例如,如果我的 Google 日历有一个从 12 月 20 日上午 10 点开始持续一个小时的事件,我的 PHP 输出将显示一个从 20 日上午 10 点开始到 21 日上午 11 点结束的事件。否则它工作正常。

我已在我的 Google 日历帐户上将我的时间设置为本地(新西兰)时间,并在 PHP 中使用 date_default_timezone_set("Pacific/Auckland");

计算完成日期的可怕行是

$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+date("Z",strtotime($ns_gd->when->attributes()->endTime)));

其中 $dateformat 是具有日期格式的字符串。

Google Calendar XML 给出了开始和结束时间

2011-12-22T10:00:00.000+13:00
2011-12-23T11:00:00.000+13:00

分别,PHP 正在计算从 2011 年 12 月 22 日上午 10 点到 2011 年 12 月 23 日下午 2 点的时间范围。

这是怎么回事?!?!

最佳答案

这一行确实很可怕:

$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+
date("Z",strtotime($ns_gd->when->attributes()->endTime)));

strtotime可以处理这种类型的 ISO 8601日期就好了。此代码片段可能是在 strtotime 取消时区并以 UTC 返回日期时间的假设下编写的,因此需要手动计算时区“校正” - 这就是 +date( “Z”, ...) 代表(第二个参数“Z”——时间戳——实际上被忽略了)。

因此在您的示例中,您的日期增加了 13 个小时。而 10:00 + 13:00 = 23:00(晚上 11 点)仍然是同一天,但是 11:00 + 13:00 = 24:00 (上午 12 点)实际上是新的一天的 00:00

所以正确的日期转换方式是:

$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime));

关于php - 使用 PHP 解析 Google Calendar XML - 结束日期相差一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8463235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com