Bug #81651 [NEW]: In a for loop using dates, a date modify also updates the initial variable
| From: | cliffie dot baker at gmail dot com | Date: | Tue, 23 Nov 2021 05:14:06 +0000 |
| Subject: | Bug #81651 [NEW]: In a for loop using dates, a date modify also updates the initial variable | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-237928@lists.php.net to get a copy of this message | ||
From: cliffie dot baker at gmail dot com
Operating system: Windows 10
PHP version: 7.4.26
Package: Date/time related
Bug Type: Bug
Bug description:In a for loop using dates, a date modify also updates the initial variable
Description:
------------
Using a for loop to generate a range of dates and using the date modify
to do the increment, the initial variable used for the start date is
also being updated. I don't think this is the correct behaviour and
certainly isn't consistent with a simple for loop.
Must admit I haven't tested in PHP 7.4.26 but have in 7.4.25
Tested using the 7.4.25 version of Xampp environment and also the 'try
it' feature of the w3schools
Test script:
---------------
$start_date = new datetime('first Monday of January 2022');
$end_date = new datetime('last Monday of January 2022');
for ($date1 = $start_date; $date1 <= $end_date; $date1->modify('+1
week')) {
echo $date1->format('Y-m-d').'<br>';
}
echo 'Start date '. $start_date->format ('Y-m-d');
Gives
2022-01-03
2022-01-10
2022-01-17
2022-01-24
2022-01-31
Start date 2022-02-07
The original start date of 2022-01-03 has been updated to 2022-02-07
through the loop
Compare this to a simple for loop
$start = 5;
for ($x = $start; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
echo 'start '.$start;
?>
Gives
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
start 5
This is as expected
Expected result:
----------------
Should be -
For a for loop with date modify
Start 2022-01-03 (the original value)
Actual result:
--------------
Actual
For a for loop with date modify
Start date 2022-02-07 (the updated value at the end of the loop)
--
Edit bug report at https://bugs.php.net/bug.php?id=81651&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=81651&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=81651&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=81651&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=81651&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=81651&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=81651&r=support
Expected behavior: https://bugs.php.net/fix.php?id=81651&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=81651&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=81651&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=81651&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81651&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=81651&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=81651&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=81651&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=81651&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=81651&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=81651&r=mysqlcfg