1

When I try to add a month to a date and it's the last day of the month, i get this weird behavior

$data = date("Y-m-d", $date);           // $date is 1643583600and $data-> 2022-01-31
$duedate = new DateTime($data);         // $duedate -> "2022-01-31 00:00:00.000000"
$duedate->modify('+1 month');           // $duedate -> "2022-03-03 00:00:00.000000"
$m = $duedate->format('m');             // $m = 03

However, the problem does not exist if:

  • I want to add a month to 2022-02-28
  • the starting date is not a month end
Zeno
  • 55
  • 9
  • This problem is talked over in the manual (Example #3). For a solution you might look at the comment there from Angelo, https://www.php.net/manual/en/datetime.add.php#118342 – ArendE Apr 13 '22 at 13:06
  • If 28th Jan + 1 month is 28th Feb, then 31st Jan + 1 month ought to be 31st Feb, but that doesn't exist, so any result other than an error message is going to seem "weird" for some use cases. You need to think carefully about what the requirements are for your particular task, and define your expected behaviour knowing that months have different lengths. – IMSoP Apr 13 '22 at 13:22

3 Answers3

2

instead of the last date of the month using the first date of the month.

date("2022-01-01") or date("Y-m-01")

$data = date("2022-01-01");           // $date is 1643583600and $data-> 2022-01-01
$duedate = new DateTime($data);         // $duedate -> "2022-01-01 00:00:00.000000"
$duedate->modify('+1 month');           // $duedate -> "2022-02-02 00:00:00.000000"
$m = $duedate->format('m');
echo $data." ".$m;

Output

2022-01-01 02
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
  • That makes perfect sense, but I think Zeno knows that. The problem here is probably not how to add a month, but more the "weird behavior" of "+1 month". How to cope, in an algorithm, with months that do not have equal length? – KIKO Software Apr 13 '22 at 12:31
  • for that, we have to talk with the PHP DateTime function maker. because it were not consider February because it has 28 or 29 days – Bhargav Chudasama Apr 13 '22 at 12:33
  • In this context one should know [How to find the last day of the month from date](https://stackoverflow.com/questions/1686724/how-to-find-the-last-day-of-the-month-from-date) – Luuk Apr 13 '22 at 13:35
0

If you're only interested in the number of the next month, you can ask for the first day of next month and you'll be fine.

Note: You can directly insert a timestamp into DateTime if you prepend it with @

$date=1643583600;
$duedate = new DateTime("@$date");
$duedate->modify("first day of next month");
echo $duedate->format('n');

output: 2

Michel
  • 4,076
  • 4
  • 34
  • 52
0

This behavior is because of the overflow of no. of days in a month it tries to add. If you wish to get only last day of each month, you can use the t symbol in date like below, which gives you no. of days in a month which is automatically the last day:

<?php

for($i = 1; $i <= 12; ++$i){
    echo date("Y-m-t", strtotime(date("Y") . "-". $i. "-01")),PHP_EOL;
}
nice_dev
  • 17,053
  • 2
  • 21
  • 35