2

This is only happening on the 31st so far

echo date('F',strtotime('this month')); //May
echo date('F',strtotime('next month'));//July
echo date('F',strtotime('+1 month'));//July

As far as I understand June comes after May. But i'm guessing php is being lazy in just adding 31 days from now, skipping an entire month.

How can I safely get the next month regardless of lenght? Ideally using strttotime

edit Forgot to mention, why i was hoping to use strtotime is that I'm using a search box to find events interpreting user input strtotime

Moak
  • 12,596
  • 27
  • 111
  • 166
  • 3
    Mentioned in the comments of the strtotime docs: http://www.php.net/manual/en/datetime.formats.relative.php#103206 – Dancrumb May 31 '11 at 04:19
  • possible duplicate of [PHP Strtotime -1month -2month](http://stackoverflow.com/questions/1211824/php-strtotime-1month-2month) – Gordon May 31 '11 at 05:46
  • 1
    Also, make sure you read the comments to http://bugs.php.net/bug.php?id=22486. It should be stressed that this is not a bug though, but expected behavior. See http://www.gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings.html#SEC120 for additional information. – Gordon May 31 '11 at 05:47

3 Answers3

7

The most robust way is probably:

date('F', mktime(0, 0, 0, date('n') + 1, 1))
deceze
  • 510,633
  • 85
  • 743
  • 889
3

You can also use:

echo date('Y-m-d', strtotime("first day of -1 month")); 
SeanDowney
  • 17,368
  • 20
  • 81
  • 90
1

This is because of the date overflow in how PHP handles the date. It just increases the month by 1. There is no 31st of June so so it resolves it by adding a day on which then puts it into July. Try converting it to the first of the month and then incrementing the month.

This is a bug, but due to the way it is and how long it's been there it's marked as wontfix. https://bugs.php.net/bug.php?id=66988

icambridge
  • 111
  • 2