Bug #52480 [Com]: Incorrect difference using DateInterval

From: Date: Mon, 13 Jun 2011 08:51:00 +0000
Subject: Bug #52480 [Com]: Incorrect difference using DateInterval
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-161362@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=52480&edit=1

 ID:                 52480
 Comment by:         petros at rufunka dot com
 Reported by:        alex dot joyce at staff dot comcen dot com dot au
 Summary:            Incorrect difference using DateInterval
 Status:             Assigned
 Type:               Bug
 Package:            Date/time related
 Operating System:   Debian 5.0.3
 PHP Version:        5.3.3
 Assigned To:        derick
 Block user comment: N
 Private report:     N

 New Comment:

The problem lies between the last day of February and first day of March. 

At the following example:
$first = new DateTime('2011-03-01');
$second = new DateTime('2011-03-29');
$interval = $second->diff($first);

will get the wrong result.

If I set my timezone to Europe/Stockholm which is +1 GMT then if i set the $first = new
DateTime(’2011-03-01 00:59:00′); I still get the wrong result. However an hour value above
or equal to +1 ie $first = new DateTime(’2011-03-01 01:00:00′); will give the correct
example.

So if you are GMT + 2 you need to have a value above or equal to 2011-03-01 02:00:00.

A quick fix, as mentioned above, is to set your timezone to UTC:
date_default_timezone_set(‘UTC’);
and in this case you match the time with the needed in order to get correct results.

Another example with the opposite results is to set your timezone to:
date_default_timezone_set(‘America/Mexico_City’);
$first = new DateTime(’2011-02-28 22:01:00′);
$second = new DateTime(’2011-03-29 03:00:00′);
then the diff will think that you are in the same month.


Previous Comments:
------------------------------------------------------------------------
[2011-04-12 16:37:51] fischer at wild-east dot de

This happens only when setting a DateTime without the time part or a time below 02:00:00. At least
for the 'Europe/Berlin' timezone.

------------------------------------------------------------------------
[2010-07-30 10:46:55] derick@php.net

This is going to be a fun one to fix :-/

------------------------------------------------------------------------
[2010-07-30 01:40:02] alex dot joyce at staff dot comcen dot com dot au

Changing the timezone shows a difference.

Australia/Sydney (default): 5 months
UTC: 4 months 28 days

------------------------------------------------------------------------
[2010-07-29 09:35:01] degeberg@php.net

With the timezone set to Europe/Copenhagen, I get the same results as submitter. When set to UTC, I
get the same results on Rasmus. This is on Ubuntu 10.04.

daniel@daniel-laptop:~$ php -v
PHP 5.3.4-dev (cli) (built: Jul 29 2010 09:30:24) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

------------------------------------------------------------------------
[2010-07-29 09:15:41] rasmus@php.net

I don't see why I can't reproduce it then.  Try adding a call to 
date_default_timezone_set() to the top of your script and work in UTC to 
eliminate local timezone issues.  

  date_default_timezone_set('UTC');
  $date_start = new DateTime('2010-03-01');
  $date_end   = new DateTime('2010-07-29');
  $interval = $date_start->diff($date_end);
  print_r($interval);

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    http://bugs.php.net/bug.php?id=52480


-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52480&edit=1


Thread (28 messages)

« previous php.bugs (#161362) next »