0

Right now if you where to use strtotime('1 month') on January 31st you would get march 1st but I wondering if there is a simple way that if I where to run that same script on the same day instead of getting march 1st I could get February 28. Is this possible? Any suggestions would be greatly appreciated.

Thanks

EDIT:

Sorry I think I did not explain this properly, I want to be able to always get 30 days except for months where that day dont exists. So for instance if it is January 31st I want to be able to get February 28th but if it was January 10 I want February 10.

Jacinto
  • 558
  • 2
  • 9
  • 24
  • 1
    possible duplicate of [PHP last day of the month](http://stackoverflow.com/questions/1686724/php-last-day-of-the-month) – mcuadros Sep 12 '14 at 19:00
  • Please see my edit its slightly more complicated then just the end of the month – Jacinto Sep 12 '14 at 19:04
  • just add 30 to your date? why do you need strtotime for that? or `strtotime(+30 day)` – bansi Sep 12 '14 at 19:05

1 Answers1

2

You could try a more verbose text:

$date = new DateTime('last day of next month');
echo $date->format('Y-m-d');
// => 2014-10-31

EDIT:

To answer your updated question, you can start with next month and compare it to last day of next month. If next month is larger than last day of next month, use last day of next month instead:

$date = new DateTime('next month');
if ($date > ($end = new DateTime('last day of next month'))) {
  $date = $end;
}
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94