Bug #81651 [Nab]: In a for loop using dates, a date modify also updates the initial variable
Edit report at https://bugs.php.net/bug.php?id=81651&edit=1
ID: 81651
User updated by: cliffie dot baker at gmail dot com
Reported by: cliffie dot baker at gmail dot com
Summary: In a for loop using dates, a date modify also
updates the initial variable
Status: Not a bug
Type: Bug
Package: Date/time related
Operating System: Windows 10
PHP Version: 7.4.26
Block user comment: N
Private report: N
New Comment:
Thanks for the prompt response. I will try your suggestion.
Sorry for the newbie question
Thank you very much.
Previous Comments:
------------------------------------------------------------------------
[2021-11-23 06:15:44] requinix@php.net
There's a pretty big difference between the number 5 and an instance of the DateTime object.
For example, one is a number and the other is an object.
$date1 and $start_date are going to be the same object because that is how objects work in PHP:
assignment does not create copies. If you want $date1 and $start_date to be two different objects
then they need to be two different objects.
You can do that by cloning $start_date into $date1.
https://www.php.net/manual/en/language.oop5.cloning.php
But don't do that here. Instead of a for loop and DateTime::modify, use a foreach loop and
DatePeriod.
https://www.php.net/manual/en/class.dateperiod.php
------------------------------------------------------------------------
[2021-11-23 05:14:06] cliffie dot baker at gmail dot com
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 this bug report at https://bugs.php.net/bug.php?id=81651&edit=1
Thread (3 messages)