|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2021-11-23 06:15 UTC] requinix@php.net
-Status: Open
+Status: Not a bug
[2021-11-23 06:15 UTC] requinix@php.net
[2021-11-23 07:03 UTC] cliffie dot baker at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 06:00:01 2025 UTC |
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)