1

I have this code

$begin_date_2021_2022='01-09-2021';
for ($i=0; $i <5 ; $i++) { 
    $begin_date_2021_2022=date('d/m/Y',strtotime('+1 month', strtotime($begin_date_2021_2022))) ; 
    echo $begin_date_2021_2022."<br>";
} 

Output:

01/10/2021<br>
10/02/2021<br>
02/11/2021<br>
11/03/2021<br>
03/12/2021<br>

My expectation is

01/10/2021<br>
01/11/2021<br>
01/12/2021<br>
01/01/2022<br>
01/02/2022<br>

Where is the problem?

Altan A.
  • 21
  • 4
  • So you want to add `30 days`? Use `+30 days`? – 0stone0 Sep 10 '21 at 15:47
  • Does this answer your question? [Why does PHP date('m', strtotime('-1 months')) not work correctly for today? 07/31](https://stackoverflow.com/questions/31750635/why-does-php-datem-strtotime-1-months-not-work-correctly-for-today-07) – 0stone0 Sep 10 '21 at 15:49
  • There's a dup somewhere, but when you use `/` it assumes American style month and day `m/d/y` you need `-` to use `d-m-y`. Or better https://www.php.net/manual/en/datetime.createfromformat.php – AbraCadaver Sep 10 '21 at 15:56

1 Answers1

0

When you use the / in the format it assumes American style date. So you need to place dashes in to the format for it to add it correctly. If you want to output it with a / you can do it the way I did it below.

$begin_date_2021_2022='01-09-2021';
for ($i=0; $i <5 ; $i++) { 
    $begin_date_2021_2022=date('d-m-Y', strtotime('+1 month',strtotime($begin_date_2021_2022))) ; 
    echo date('d/m/Y',strtotime($begin_date_2021_2022))."<br>";
} 

reference https://www.php.net/manual/en/function.strtotime.php

Drivers Sea
  • 446
  • 2
  • 10