Bug #52480 [Fbk->Opn]: Incorrect difference using DateInterval

From: Date: Thu, 29 Jul 2010 07:35:02 +0000
Subject: Bug #52480 [Fbk->Opn]: Incorrect difference using DateInterval
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-152912@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
 Updated by:         degeberg@php.net
 Reported by:        alex dot joyce at staff dot comcen dot com dot au
 Summary:            Incorrect difference using DateInterval
-Status:             Feedback
+Status:             Open
 Type:               Bug
 Package:            Date/time related
 Operating System:   Debian 5.0.3
 PHP Version:        5.3.3
 Block user comment: N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[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);

------------------------------------------------------------------------
[2010-07-29 08:52:20] alex dot joyce at staff dot comcen dot com dot au

28/29, it doesn't matter. I'm only interested in the months.

I've tried:

1) FreeBSD 6, PHP 5.3.2
2) Debian 4, PHP 5.3.2
3) Debian 6, PHP 5.3.2/PHP 5.3.3

I upgraded that last one to submit this bug report, same fault.

All compiled from source.

------------------------------------------------------------------------
[2010-07-29 08:27:50] rasmus@php.net

Don't you mean 4 months and 28 days in the expected results?  7-3=4,
29-1=28
And that is exactly what I get on my Debian box with PHP 5.3.3:


php >   $date_start = new DateTime('2010-03-01');
php >   $date_end   = new DateTime('2010-07-29');
php > 
php >   $interval = $date_start->diff($date_end);
php > 
php >   print_r($interval);
DateInterval Object
(
    [y] => 0
    [m] => 4
    [d] => 28
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 150
)

Are you using 5.3.3 built from our sources or are you using a
Debian-packaged 
version?

------------------------------------------------------------------------
[2010-07-29 04:49:26] alex dot joyce at staff dot comcen dot com dot au

Description:
------------
Trying to calculate the month difference between two dates accurately.

Test script:
---------------
<?php

  $date_start = new DateTime('2010-03-01');
  $date_end   = new DateTime('2010-07-29');

  $interval = $date_start->diff($date_end);

  print_r($interval);

Expected result:
----------------
DateInterval Object
(
    [y] => 0
    [m] => 4
    [d] => 29
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 150
)

Actual result:
--------------
DateInterval Object
(
    [y] => 0
    [m] => 5
    [d] => 0
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 150
)


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



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


Thread (28 messages)

« previous php.bugs (#152912) next »