From: lopez dot fernandez dot jorge at gmail dot com
Operating system: Ubuntu 9.04
PHP version: 5.3.0
PHP Bug Type: Date/time related
Bug description: DateTime::diff() repeats previous sub() operation
Description:
------------
When you call the 'diff' function onto or from a DateTime object that did
a 'sub' operation before, it will substract again the same amount of time
from the DateTime object before calculating the difference, so neither the
result nor the DateTime object will have the correct values.
Reproduce code:
---------------
<?php
$date1 = date_create("2009-03-27");
$date2 = date_create("2009-03-01");
print "\$date1 at init: " . $date1->format("Y-m-d") . "\n";
print "\$date2 at init: " . $date2->format("Y-m-d") . "\n";
$diff = $date1->diff($date2);
print "\$date1 after first diff: " . $date1->format("Y-m-d") . "\n";
print "\$diff->days after first diff: " . $diff->days . "\n";
$date1 = $date1->sub(new DateInterval("P2D"));
print "\$date1 after sub: " . $date1->format("Y-m-d") . "\n";
$diff = $date1->diff($date2);
print "\$date1 after second diff (called at \$date1): " .
$date1->format("Y-m-d") . "\n";
print "\$diff->days after second diff: " . $diff->days . "\n";
$diff = $date2->diff($date1);
print "\$date1 after third diff (called at \$date2): " .
$date1->format("Y-m-d") . "\n";
print "\$diff->days after third diff: " . $diff->days . "\n";
?>
Expected result:
----------------
$date1 at init: 2009-03-27
$date2 at init: 2009-03-01
$date1 after first diff: 2009-03-27
$diff->days after first diff: 26
$date1 after sub: 2009-03-25
$date1 after second diff (called at $date1): 2009-03-25
$diff->days after second diff: 24
$date1 after third diff (called at $date2): 2009-03-25
$diff->days after third diff: 24
Actual result:
--------------
$date1 at init: 2009-03-27
$date2 at init: 2009-03-01
$date1 after first diff: 2009-03-27
$diff->days after first diff: 26
$date1 after sub: 2009-03-25
$date1 after second diff (called at $date1): 2009-03-23
$diff->days after second diff: 22
$date1 after third diff (called at $date2): 2009-03-21
$diff->days after third diff: 20
--
Edit bug report at http://bugs.php.net/?id=49059&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=49059&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=49059&r=trysnapshot53
Try a snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=49059&r=trysnapshot60
Fixed in SVN:
http://bugs.php.net/fix.php?id=49059&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=49059&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=49059&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=49059&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=49059&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=49059&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=49059&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=49059&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=49059&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=49059&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=49059&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49059&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=49059&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=49059&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=49059&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=49059&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=49059&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=49059&r=mysqlcfg