php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49059 DateTime::diff() repeats previous sub() operation
Submitted: 2009-07-26 00:12 UTC Modified: 2010-03-07 18:25 UTC
Votes:4
Avg. Score:4.0 ± 0.7
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:1 (25.0%)
From: lopez dot fernandez dot jorge at gmail dot com Assigned: derick
Status: Closed Package: Date/time related
PHP Version: 5.3.0 OS: Ubuntu 9.04
Private report: No CVE-ID:
 [2009-07-26 00:12 UTC] lopez dot fernandez dot jorge at gmail dot com
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-19 13:24 UTC] yoarvi at gmail dot com
date_sub in php_date.c doesn't reset dateobj->time->have_relative.

The following patch (against 5.3 SVN) includes a fix and a test case:

Index: ext/date/php_date.c
===================================================================
--- ext/date/php_date.c	(revision 293574)
+++ ext/date/php_date.c	(working copy)
@@ -2861,6 +2861,8 @@
 	timelib_update_ts(dateobj->time, NULL);
 	timelib_update_from_sse(dateobj->time);
 
+	dateobj->time->have_relative = 0;
+
 	RETURN_ZVAL(object, 1, 0);
 }
 /* }}} */
Index: ext/date/tests/bug49059.phpt
===================================================================
--- ext/date/tests/bug49059.phpt	(revision 0)
+++ ext/date/tests/bug49059.phpt	(revision 0)
@@ -0,0 +1,35 @@
+--TEST--
+Bug #49059 (DateTime::diff() repeats previous sub() operation)
+--FILE--
+<?php
+date_default_timezone_set('Asia/Calcutta');
+
+$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";
+?>
+--EXPECT--
+$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
+
 [2010-03-07 18:25 UTC] derick@php.net
-Status: Assigned +Status: Closed
 [2010-03-07 18:25 UTC] derick@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2010-03-07 18:25 UTC] derick@php.net
Automatic comment from SVN on behalf of derick
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=295933
Log: - Fixed bug #49059 (DateTime::diff() repeats previous sub() operation).
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 18:01:55 2014 UTC