xydboom
xydboom

Reputation: 13

PHP Date -1 month today not working

Im having a problem with php date subtracting 1 month in the current date

i got this code.

$date = date("Y-m-d H:i:s", strtotime("-1 month"));

echo $date;

with a result of this? am i missing something.

2016-03-01 23:21:20

Upvotes: 1

Views: 112

Answers (2)

Barmar
Barmar

Reputation: 781726

Today is March 30. 1 month ago was February 30. But February only had 29 days this year, so February 30 is the same as March 1.

You'll run into the problem on the following days, because that day of the month doesn't exist in the previous month:

  • March 29 (in non-leap years)
  • March 30-31
  • May 31
  • July 31
  • October 31
  • December 31

Upvotes: 4

If you go back one month from 2016-03-30, you get 2016-02-30. However, 2016-02-29 was the last day of February, so it resolves that date to mean 2016-03-01.

Upvotes: 0

Related Questions