Why is the PHP date ('m', strtotime ('- 1 months')) not working correctly today? 07/31

I have a script that gets the current and last month in PHP like this:

$currentMonth = date('m'); //Expected:07 //Result:07 $lastMonth = date('m', strtotime('-1 months')); //Expected:06 //Result:07 

Today is 31 or the end of July. Can this result be expected from PHP?

When using -31 days, the result will be as expected:

 $lastMonth = date('m', strtotime('-31 days')); //Expected:06 //Result:06 
+6
source share
5 answers

Here's a cleaning test that does not expire:

 <?php $origin = mktime(18, 0, 0, 7, 31, 2015); var_dump( date('r', $origin), date('r', strtotime('-1 months', $origin)) ); 
 string(31) "Fri, 31 Jul 2015 18:00:00 +0200" string(31) "Wed, 01 Jul 2015 18:00:00 +0200" 

I am sure that this is a problem with the documentation, because the manual clearly states this (primary focus):

The relative values ​​of the month are calculated depending on the length of the months that they pass. An example is "+2 month 2011-11-30", which will produce "2012-01-30". This is due to November 30 days in length, and December - 31 days in length, just 61 days.

... and this is wrong.

PHP Error Tracking The tone is deceiving it. They are all closed, if not a mistake. Here is the corresponding comment from 2009 that explains this:

I agree that this is annoying behavior.

In addition, implementation is problematic. Basically , if you use '+1 month', it takes a month, adds 1 and analyzes the result as a new date .

If you use “+1 month” in the first month of the month, it sets the date in the next first month.

This behavior gives the impression that php takes into account the length of the month, which is incorrect.

But if you use "+1 month" on the last day of the month, the result is unexpected , since 2009-05-31 becomes 2009-06-31, which is an invalid date and then interpreted as 2009-07-01.

This should at least be mentioned in the documentation .

+2
source

You can do it

 $d = new DateTime(); $currentMonth = $d->format('m'); //Expected:07 //Result:07 print $currentMonth; $d->modify('first day of previous month'); print "<br/>"; $lastMonth = $d->format('m'); //Expected:06 //Result:06 print $lastMonth; 

DEMO: http://codepad.viper-7.com/kokWi8

+2
source

This is a problem with the date-string PHP parser. Take a look here: http://derickrethans.nl/obtaining-the-next-month-in-php.html

@Mr. Llama made a script showing what other dates this issue affects: http://codepad.viper-7.com/E4gP0W

The solution I went with:

 //Date:07/31/15 $currentMonth = date('m'); //Result:07 $lastMonth = date('m', strtotime('first day of -1 months')); //Result:06 
+2
source

-1 month interpreted as "the same day of the month, last month." If this day does not exist, the date will move to the next month. Actually the result is the same as strtotime("31.6.2015") - try it!

+1
source

There are more s in the month. It should be like this:

 $lastMonth = date('m', strtotime('-1 month')); 
-1
source

All Articles