[php-src] Issue #13773: DatePeriod not taking into account microseconds for end date
| From: | mdkieran | Date: | Thu, 21 Mar 2024 09:47:19 +0000 |
| Subject: | [php-src] Issue #13773: DatePeriod not taking into account microseconds for end date | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246628@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/13773
Author: mdkieran
### Description
The following code:
```php
$period = new DatePeriod(
new DateTime('2000-01-01 12:00:00'),
new DateInterval('P1D'),
new DateTime('2000-01-02 12:00:00.000001'),
);
foreach ($period as $date) {
echo $date->format('Y-m-d H:i:s') . '<br/>';
}
```
Resulted in this output:
```
2000-01-01 12:00:00
```
But I expected this output instead:
```
2000-01-01 12:00:00
2000-01-02 12:00:00
```
My understanding is that if you're going to yield a new date from the DatePeriod, as long as
it's less than the end date, then it's allowed, otherwise you've reached the end. So
whether the end date is 1 microsecond or 1 whole second greater than a would-be newly yielded date,
it is still greater than it and ought be allowed.
The reason this is a problem is because if you're making DateTime objects "now" then
microseconds are included automatically, so if you want this to work properly, you'll need to
remove the microseconds when setting the end date.
The only information I could find about this was from this comment: https://php.vn.ua/manual/en/class.dateperiod.php#124147
### PHP Version
PHP 8.2.15
### Operating System
Fedora Linux 38 (Workstation Edition)