PHP Strtotime -1month -2month

This worked fine yesterday, with no code changes.

echo date("M", strtotime("-3 month", time()) ); echo date("M", strtotime("-2 month", time()) ); echo date("M", strtotime("-1 month", time()) ); echo date("M", time()); 

The output he made yesterday was what you would expect - a.u., May, June, July

Today it repeats May May July July

Any ideas?

Thanks in advance.

+11
php strtotime
Jul 31 '09 at 10:31
source share
6 answers

This may be due to error # 44073

You can try something like this:

 echo date("M", strtotime("-3 month", strtotime(date("F") . "1")) ) . "\n"; echo date("M", strtotime("-2 month", strtotime(date("F") . "1")) ) . "\n"; echo date("M", strtotime("-1 month", strtotime(date("F") . "1")) ) . "\n"; echo date("M", time()) . "\n"; 

(The solution is found in the comment section of strtotime ; direct link )

And the conclusion:

 Apr May Jun Jul 

Type of "cheating" with the date format and the name of the month and all this ...

+11
Jul 31 '09 at 10:38
source share

Gorden correctly identified the problem, but I wanted to give another solution that would be useful, not technical. Just use "first day" or "last day" in your strtotime. For example, the following examples overcome the problem on the 31st of a month:

 // Today is May 31st //All the following return 2012-04-30 echo date('Ym-d', strtotime("last day of -1 month")); echo date('Ym-d', strtotime("last day of last month")); echo date_create("last day of -1 month")->format('Ym-d'); // All the following return 2012-04-01 echo date('Ym-d', strtotime("first day of -1 month")); echo date('Ym-d', strtotime("first day of last month")); echo date_create("first day of -1 month")->format('Ym-d'); 
+7
May 31 '12 at 16:24
source share

Try this instead of strtotime :

 mktime(0, (date("n") - 3 + 12) % 12, 1) 

The idea is to take the current month number ( date("n") ), subtract the number of months you want from it (here -3 ), add 12 to it, and then get modulo 12.

+4
Jul 31 '09 at 10:40
source share

This is a known and expected behavior. The reason for this is because the month is a relative date without a fixed length. From http://www.gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings.html#SEC120

The time offset unit can be selected on the line "year" or "month" to move in whole years or months. These are fuzzy units, since the years and months are not the same. More precise units are β€œtwo weeks that cost 14 days,” a week costs 7 days, β€œa day costs 24 hours,” β€œan hour costs 60 minutes,” β€œminute” or β€œmin,” 60 seconds, and β€œsecond” or β€œ second in one second. " The suffix on these devices is accepted and ignored.

Therefore (my attention)

Fuzz in units can cause problems with relative elements. For example, "2003-07-31 -1 month can be evaluated until 2003-07-01, since 2003-06-31 is an invalid date. To more reliably determine the previous month, you can request the month before the 15th day of the current month . For example:

 $ date -R Thu, 31 Jul 2003 13:02:39 -0700 $ date --date='-1 month' +'Last month was %B?' Last month was July? $ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!' Last month was June! 

The above can be expressed in PHP as

 echo date('M', strtotime(date('Ym-') . '15 -1 month')); 

Also see points below http://bugs.php.net/bug.php?id=22486

+3
May 31 '11 at 6:10 a.m.
source share

From PHP 5.3 (Link: https://bugs.php.net/bug.php?id=44073 ) and above you can do:

" first day of +1 month " or " first day of next month " or even " last day of next month "

Example:

 $date = '2015-12-31'; echo date("Ymd", strtotime($date ."first day of previous month")); 

Will produce: 2015-11-01 This does not calculate days based on a time of +30 days.

+2
Mar 05 '16 at 12:52 on
source share

an easy way would be to write this type

echo date ("M", strtotime ("this month +3 months"));

or

echo date ("M", strtotime ("this month -3 months"));

Depends on how you use it.

0
Mar 18 2018-12-18T00:
source share



All Articles