I would like to loop from a given date through all the days until the current date, but not including the current date.
Piecing together code that I found on the Internet, I came up with the following solution, which does almost what I want:
$date_begin = new DateTime("2005-03-11");
$today = new DateTime("now");
for ($i = $date_begin; $i <= $today; $i->modify("+1 day")) {
...
}
This code loops through all the days from the given date until the current date, including the current date. In order to exclude the current date, I would have thought that I merely need to replace $i <= $today
by $i < $today
. But this does not work. Even with <
instead of <=
, the current date is included in the loop. Why is that? What am I misunderstanding? And how can I exclude the current date?
Wolfhart is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.