[英]php datetime->diff is calculating wrong amount of hours (3 too much)
我在php中有以下DateTime对象:
[start1] => DateTime Object (
[date] => 2012-05-21 12:59:59
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[end1] => DateTime Object (
[date] => 2012-05-21 22:36:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)
以及结果:
$time->end1->diff($time->start1
是:
DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 12 [i] => 36 [s] => 2 [invert] => 1 [days] => 0 )
为什么我会得到12小时而不是9小时?
我找到了解决方案......在做diff之前,我在start1对象上做了一个sub()。 现在我已经看到了为什么我的结果是假的......这是答案,但我真的不知道为什么会这样。 http://www.php.net/manual/en/datetime.sub.php#101175
我知道这实际上可能是你创建两个DateTime对象的方法,但我想我会把对我有用的东西看看是否可以帮到你。
要创建我做的两个对象:
$start1 = new DateTime('2012-05-21 12:59:59', new DateTimeZone('Europe/Berlin'));
$end1 = new DateTime('2012-05-21 22:36:00', new DateTimeZone('Europe/Berlin'));
并打印出两个对象和diff:
print_r($end1->diff($start1));
我有:
DateTime Object
(
[date] => 2012-05-21 12:59:59
[timezone_type] => 3
[timezone] => Europe/Berlin
)
DateTime Object
(
[date] => 2012-05-21 22:36:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 9
[i] => 36
[s] => 1
[invert] => 1
[days] => 0
)
我看到start1和end1对象的两个输出都是相同的,但我的差异反映了正确的9小时差异。 也许有关于如何在PHP版本中创建DateTime对象的问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.