php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50055 indeterminant DateInterval does not subtract correctly
Submitted: 2009-11-02 15:07 UTC Modified: 2010-03-07 18:56 UTC
From: jennifer dot kimball at nrc dot ca Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3.0 OS: Solaris 10
Private report: No CVE-ID: None
 [2009-11-02 15:07 UTC] jennifer dot kimball at nrc dot ca
Description:
------------
The logical behaviour of a DateInterval created with the "next" keyword, when subtracted from a DateTime, should be to use "last" instead of "next" in the calculation (and vice versa).

Instead, it appears that DateIntervals created with "next" or "last" keywords cannot be subtracted from DateTimes with any accuracy. A DateTime results from the operation but has an unexpected value.

Reproduce code:
---------------
//positive DateInterval
$da1=date_create();
$ds1=date_create();
$i=DateInterval::createFromDateString('third Tuesday of next month');
echo $da1->format('Y-m-d');
echo date_add($da1,$i)->format('Y-m-d');//works
echo date_sub($ds1,$i)->format('Y-m-d');//fails with weird date

//negative DateInterval
$da2=date_create();
$ds2=date_create();
$i2=DateInterval::createFromDateString('third Tuesday of last month');
echo $da2->format('Y-m-d');
echo date_add($da2,$i2)->format('Y-m-d');//works
echo date_sub($ds2,$i2)->format('Y-m-d');//fails with weird date

Expected result:
----------------
2009-11-02 //today
2009-12-15 //third Tuesday of December
2009-10-20 //third Tuesday of October

2009-11-02 //today
2009-10-20 //third Tuesday of October
2009-12-15 //third Tuesday of December

Actual result:
--------------
2009-11-02 //today
2009-12-15 //third Tuesday of December
2009-09-18 //third Friday of September

2009-11-02 //today
2009-10-20 //third Tuesday of October
2009-11-18 //third Wednesday of November

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-02 16:01 UTC] derick@php.net
There is a bug here, but the only thing would be that "sub" can *not* be used with relative time strings such as "last ... of". It is logically too complicated (for people) to easily reverse strings like that. Such an attempt should throw an exception/error instead though.
 [2010-03-07 14:55 UTC] derick@php.net
-Status: Assigned +Status: Closed
 [2010-03-07 14:55 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.

Fixed by making this throw a warning, as it's not supported to use "special" relative time strings with ->sub().
 [2010-03-07 18:56 UTC] jani@php.net
Derick had a small typo in his commit, here's the commit:

Revision: http://svn.php.net/viewvc?view=revision&revision=295924
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 07:01:29 2024 UTC