1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00
Files
archived-php-src/ext/date/tests/DatePeriod_no_advance_on_valid.phpt
Ilija Tovilo 8a699372f2 Fix flaky DatePeriod test
$start and $end use the H:i:s from the current time. If $end happens on
a second boundary, $start + 4 days will include $end, thus performing an
extra iteration. Fix this by setting H:i:s to 00:00:00.
2025-03-06 15:01:30 +01:00

53 lines
1.1 KiB
PHP

--TEST--
Date Period iterators do not advance on valid()
--FILE--
<?php
$start = DateTime::createFromFormat('Y-m-d H:i:s', '2022-01-01 00:00:00');
$end = DateTime::createFromFormat('Y-m-d H:i:s', '2022-01-04 00:00:00');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
$iterator = $period->getIterator();
foreach ($iterator as $item) {
echo $item->format('Y-m-d') . "\n";
}
echo "---------STEP 2\n";
foreach ($iterator as $item) {
$iterator->valid();
echo $item->format('Y-m-d') . "\n";
}
$period = new DatePeriod($start, $interval, $end, DatePeriod::EXCLUDE_START_DATE);
$iterator = $period->getIterator();
echo "---------STEP 3\n";
foreach ($iterator as $item) {
echo $item->format('Y-m-d') . "\n";
}
echo "---------STEP 4\n";
foreach ($iterator as $item) {
$iterator->valid();
echo $item->format('Y-m-d') . "\n";
}
?>
--EXPECT--
2022-01-01
2022-01-02
2022-01-03
---------STEP 2
2022-01-01
2022-01-02
2022-01-03
---------STEP 3
2022-01-02
2022-01-03
---------STEP 4
2022-01-02
2022-01-03