php interval and timezone

I would like to get the date time interval between two dates in a time zone that uses daylight savings time.

The following snipet shows the problem.

$timezone = new DateTimeZone('UTC');
$from_date_obj = DateTime::createFromFormat('Y-m-d H:i:s', '2014-10-31 23:59:59',$timezone);
$to_date_obj = DateTime::createFromFormat('Y-m-d H:i:s', '2014-11-20 23:47:02',$timezone);
$interval = $from_date_obj->diff($to_date_obj, TRUE);
print_r($interval);

For UTC timezone this shows:

DateInterval Object
(
[y] => 0
[m] => 0
[d] => 19
[h] => 23
[i] => 47
[s] => 3
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 19
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)

Now this one:

$timezone = new DateTimeZone('America/Los_Angeles');
$from_date_obj = DateTime::createFromFormat('Y-m-d H:i:s', '2014-10-31 23:59:59',$timezone);
$to_date_obj = DateTime::createFromFormat('Y-m-d H:i:s', '2014-11-20 23:47:02',$timezone);
$interval = $from_date_obj->diff($to_date_obj, TRUE);
print_r($interval);

shows:

DateInterval Object 
(
[y] => 0
[m] => 0
[d] => 20
[h] => -1
[i] => 47
[s] => 3
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 19
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)

the one in UTC shows 19 days/23 hours/47 minutes and 3 seconds. The one for PT shows 20 days/-1 hours/47 minutes and 3 seconds. I think the right answer for PT should be 20 days/0 hours/47 minutes and 3 seconds.

Is there any way to do a diff in the pacific time zone and not end up with the negative hour? Is the -1 hours indication the desired result and what does it mean exactly?


update: I found out that indeed PHP doesn't seem to handle the diff with respect to DST which is discussed here:

PHP's DateTime::Diff gets it wrong?

As far as the results I was getting it appears to be a PHP bug as indicated in the above link.


What php version are you using ? Im getting this result for 2nd code block -

DateInterval Object ( 
[y] => 0 
[m] => 0 
[d] => 19 
[h] => 23 
[i] => 47 
[s] => 3 
[weekday] => 0 
[weekday_behavior] => 0 
[first_last_day_of] => 0 
[invert] => 0 
[days] => 19 
[special_type] => 0 
[special_amount] => 0 
[have_weekday_relative] => 0 
[have_special_relative] => 0 
)

both of the codes should display the same result as your comparing the same date with just different timezone each time


There are two issues raised here: One has to do with the difference between two dates and times when there is a clock change due to DST (Daylight Savings Time). In reality the difference between two dates and times should take into account the clock change, but apparently it does not.

That is documented pretty well in the following link:

PHP's DateTime::Diff gets it wrong?

Further I am chalking up the -1 problem as a php bug. Since PHP doesn't handle the DST changes between two date times anyway, and in my case the difference without taking DST into effect is close enough anyway, I will just do the diff calculation in UTC.

链接地址: http://www.djcxy.com/p/10118.html

上一篇: PHP date()和time()提前3分钟

下一篇: php间隔和时区