php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #52015 Ending date is ignored while iterating DatePeriod
Submitted: 2010-06-07 13:56 UTC Modified: 2022-05-20 10:19 UTC
Votes:14
Avg. Score:3.9 ± 0.9
Reproduced:10 of 11 (90.9%)
Same Version:4 (40.0%)
Same OS:6 (60.0%)
From: ko dot fivestar at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3.2 OS: Ubuntu 10.04
Private report: No CVE-ID: None
 [2010-06-07 13:56 UTC] ko dot fivestar at gmail dot com
Description:
------------
When passing DateTime objects to first and third argument of DatePeriod constructor, the specified date on the third argument is not being interated when both DateTime object contains same time value.

After looking through ext/date/php_date.c, date_period_it_has_more() function (line 1831) had following code:

---------------
return object->start->sse < object->end->sse ? SUCCESS : FAILURE;
---------------

While the operator "<" is currently used to compare values, operator "<=" is more appropriate for this content.

Test script:
---------------
$period = new \DatePeriod(
    new \DateTime('2010-06-01'),
    new \DateInterval('P1D'),
    new \DateTime('2010-06-03')
);

foreach ($period as $day) {
    echo $day->format('Y-m-d') . "\n";
}


Expected result:
----------------
2010-06-01
2010-06-02
2010-06-03

Actual result:
--------------
2010-06-01
2010-06-02

Patches

php_bug52015.diff (last revision 2010-06-07 14:03 UTC by degeberg@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-07 16:03 UTC] degeberg@php.net
Whether or not it's a bug is debatable. It could be considered a half-open interval (which would then make it a documentation issue).

Changing it to a closed interval would break backwards compatibility. It would probably be better adding one more option to DatePeriod that allows the inclusion of the end date. The attached patch implements that.
 [2010-06-07 16:03 UTC] degeberg@php.net
The following patch has been added/updated:

Patch Name: php_bug52015.diff
Revision:   1275919428
URL:        http://bugs.php.net/patch-display.php?bug=52015&patch=php_bug52015.diff&revision=1275919428
 [2010-06-08 11:51 UTC] tony2001@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: derick
 [2014-01-29 11:38 UTC] boogerlad at gmail dot com
Now that this option exists, how would you use both DatePeriod::EXCLUDE_START_DATE and DatePeriod::INCLUDE_END_DATE?
 [2016-01-22 12:57 UTC] aivaras at voveris dot eu
Exactly same issue as described by ko dot fivestar at gmail dot com happens on OSX PHP v5.6.11

php -v
PHP 5.6.11 (cli) (built: Jul 10 2015 22:36:04) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

Is there any estimate on when this is going to be resolved?
 [2017-10-24 07:52 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: derick +Assigned To:
 [2020-04-02 09:13 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2020-04-02 09:13 UTC] cmb@php.net
I can't assess what would be the proper behavior, given that
DatePeriod appears to be quirky in this regard[1], but at the very
least the current behavior should be documented.

[1] <https://3v4l.org/1kYQI>
 [2022-05-13 13:29 UTC] derick@php.net
-Assigned To: +Assigned To: derick
 [2022-05-13 13:29 UTC] derick@php.net
https://github.com/php/php-src/pull/8550 — for inclusion into PHP 8.2.
 [2022-05-20 10:18 UTC] git@php.net
Automatic comment on behalf of derickr
Revision: https://github.com/php/php-src/commit/c0c801d23813f6d485e64ea866654ff0798517e0
Log: Fixed bug #52015 (Allow including end date in DatePeriod iterations)
 [2022-05-20 10:18 UTC] git@php.net
-Status: Verified +Status: Closed
 [2022-05-20 10:19 UTC] derick@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at
http://www.php.net/downloads.php

For PHP 8.2.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 04:01:31 2024 UTC