PHP的DateTime :: Diff错误? [英] PHP's DateTime::Diff gets it wrong?

查看:130
本文介绍了PHP的DateTime :: Diff错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateTime :: Diff应计算适当的时间间隔,并考虑夏令时(DST)和闰年。虽然显然不是这样。恐怖代码:

DateTime::Diff should calculate a proper interval and take into account Daylight Savings Time (DST) and leap years. Although apparently it isn't so. Code of horror:

$d1 = new DateTime("2011-10-30 01:05:00", new DateTimeZone("Europe/Stockholm"));
$d2 = new DateTime("2011-10-30 03:05:00", new DateTimeZone("Europe/Stockholm"));

echo $d1->getOffset() / (60 * 60);

打印'2'!请记住,UTC时间= 1h - 2h =前一天的23:05:00。

Prints '2'! Keep in mind thus that UTC time = 1h - 2h = 23:05:00 the day before.

echo $d2->getOffset() / (60 * 60);

打印'1'。 DST发生了。 UTC时间= 3h - 1h = 02:05:00。

Prints '1'. DST happened. UTC time = 3h - 1h = 02:05:00.

$di = $d1->diff($d2);
echo "Hours of DateInterval: " . $di->h;

打印'2'!错误?

$hoursofdiff = ($d2->getTimeStamp() - $d1->getTimeStamp()) / 60 / 60;
echo "Calculated difference in hours: $hoursofdiff";

打印'3'!正确?

当时钟在给定的日期转为03:00:00时,所有瑞典人将他们的时钟转回一小时至02:00:00。这意味着从01:05到03:05之间的总金额是三个小时,就像在使用UNIX TimeStamp时手动计算回显一样。如果我们使用模拟时钟,就像我们的手指一样计算。当我们计算两个UTC时间戳之间的差异时,我使用了PHP自己的偏移量(!)的差异,更是如此。

When the clock turned 03:00:00 at the given date, all Swedes turned their clock back one hour to 02:00:00. That means that total amount passed between 01:05 until 03:05 is three hours, much like the manual calculation echo'ed when using the UNIX TimeStamp. And much like we calculates on our fingers if we use an analogue clock. Even more so when we calculate the difference between the two UTC timestamps I got using PHP's own logic of offsets (!).

是PHP还是让我的大脑停止工作正确的你们中的任何人的谴责,这个网站上存在的所有神将使我如此高兴!

Is it PHP or have my brain ceased to work properly? A reprimand from anyone of you all gods that exist on this site would make me so happy!

我在Apache服务器上使用PHP 5.4(VC9)。不幸的是,我使用Windows 7 x64作为操作系统。我测试了我的设置,反对PHP的Date / Time类中的所有错误声明(有一些与Windows有关),并且可以确认我的系统没有。除了上述代码之外,我还没有发现任何其他错误。我几乎验证了所有代码,并输出了PHP建筑师的日期和时间编程指南一书。所以我必须得出结论,它必须是我的大脑巫婆已经默认了,但我以为我会先在这里给它拍一张。

I'm using PHP 5.4 (VC9) on an Apache-server. Unfortunately I use Windows 7 x64 as OS. I have tested my setup against all claims of bugs in PHP's Date/Time classes (there are a couple related to Windows) and can confirm that my system have none of them. Except from the above stated code I have not found any other errors. I pretty much validated all code and output the book "PHP Architect's guide to date and time programming" had to offer. Therefore I must conclude it has to be my brain witch has defaulted but I thought I'd give it a shoot here first.

推荐答案

你是对的,PHP目前不处理DST转换...

You're right, PHP currently doesn't handle DST transitions...

错误报告#51051 (仍然打开)和#55253 (在PHP 5.3.9中修复)描述您遇到的问题。

Bug reports #51051 (still open) and #55253 (fixed in PHP 5.3.9) describe the problems you're having.

Daniel Convissor已经写过一个RFC尝试解决问题的一段时间,但更改日志并不表示这已被解决。我希望这将在5.4中修复,但我没有看到任何证据。

Daniel Convissor has written an RFC trying to address the issue a while back but the change-logs don't suggest this has been addressed. I was hoping this would fixed in 5.4 but I don't see any evidence it has been.

当/如果它被实现,它看起来像你必须将DST或ST附加到时间字符串。

When/if it is implemented, it looks like you'll have to append "DST" or "ST" to the time string.

最佳做法是在UTC 中执行所有的日期计算,从而避免了问题。

Best practice is to do all your date calculations in UTC, which avoids this problem.

这个DST最佳做法帖子也非常翔实。

这篇关于PHP的DateTime :: Diff错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆