#49081 [NEW]: DateTime::diff calculates diffs incorrectly
| From: | nate at frickenate dot com | Date: | Mon, 27 Jul 2009 22:55:24 +0000 |
| Subject: | #49081 [NEW]: DateTime::diff calculates diffs incorrectly | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-140347@lists.php.net to get a copy of this message | ||
From: nate at frickenate dot com
Operating system:
PHP version: 5.3.0
PHP Bug Type: Date/time related
Bug description: DateTime::diff calculates diffs incorrectly
Description:
------------
DateTime::diff calculates diffs incorrectly.
As an example, the diff of 2009-01-01 and 2009-03-31 *should be* "2
months, 30 days". 2009-01-01 + 2 months = 2009-03-01, + 30 days =
2009-03-31. Taking 2009-01-01 and using DateTime::add() to add 'P2M30D'
does indeed result in 2009-03-31. This is correct.
However, running the diff() of 2009-01-01 and 2009-03-31 returns "3
months, 2 days". add()ing 2009-01-01 + 'P3M2D' returns 2009-04-03 instead
of 2009-03-31 (as it should, since the diff that told us to add 3M2D was
incorrect).
Reproduce code:
---------------
<?
$jan_1 = new DateTime('2009-01-01');
$mar_31 = new DateTime('2009-03-31');
var_dump(date_diff($jan_1, $mar_31));
var_dump($jan_1->add(new DateInterval('P2M30D'))); // correct period
var_dump($jan_1->add(new DateInterval('P3M2D'))); // incorrect period
// END EXAMPLE CODE - following is just extra fluff
// here's the replacement function I am currently
// using to calculate the correct diff until the
// built-in method is patched and functional
function date_diff2 ($t1, $t2) {
if (! preg_match('/^\d+\z/', $t1) && ($t1 = strtotime($t1)) ===
false)
return false;
if (! preg_match('/^\d+\z/', $t2) && ($t2 = strtotime($t2)) ===
false)
return false;
if ($t1 > $t2)
list($t1, $t2) = array($t2, $t1);
$diffs = array(
'years' => 0, 'months' => 0, 'days' => 0,
'hours' => 0, 'minutes' => 0, 'seconds' => 0,
);
foreach (array_keys($diffs) as $interval) {
while ($t2 >= ($t3 = strtotime("+1 ${interval}", $t1))) {
$t1 = $t3;
++$diffs[$interval];
}
}
return $diffs;
}
?>
Expected result:
----------------
object(DateInterval)#3 (8) {
["y"]=>
int(0)
["m"]=>
int(2)
["d"]=>
int(30)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
int(89)
}
object(DateTime)#1 (3) {
["date"]=>
string(19) "2009-03-31 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "America/Montreal"
}
object(DateTime)#1 (3) {
["date"]=>
string(19) "2009-07-03 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "America/Montreal"
}
Actual result:
--------------
object(DateInterval)#3 (8) {
["y"]=>
int(0)
["m"]=>
int(3)
["d"]=>
int(2)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
int(89)
}
object(DateTime)#1 (3) {
["date"]=>
string(19) "2009-03-31 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "America/Montreal"
}
object(DateTime)#1 (3) {
["date"]=>
string(19) "2009-07-03 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "America/Montreal"
}
--
Edit bug report at http://bugs.php.net/?id=49081&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=49081&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=49081&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=49081&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=49081&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=49081&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=49081&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=49081&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=49081&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=49081&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=49081&r=support
Expected behavior: http://bugs.php.net/fix.php?id=49081&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=49081&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=49081&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=49081&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49081&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=49081&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=49081&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=49081&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=49081&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=49081&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=49081&r=mysqlcfg