1

I need to take an existing date from a variable and display a date that is always the 1st day (01) of whatever the next month is (and accounting for the year as well).

So if I have this:

$date = '2017-03-17'; // YYYY-MM-DD

I need to take that date and make it output this:

2017-04-01 // The first day of the next month

Just another example...

$date = '2017-12-23'; // YYYY-MM-DD

...should be converted to...

2018-01-01 // The first day of the next month
User_FTW
  • 504
  • 1
  • 16
  • 44
  • I think this may have already been asked and answered [here](http://stackoverflow.com/questions/1777384/how-to-find-first-day-of-the-next-month-and-remaining-days-till-this-date-with-p) – PhilS May 09 '17 at 07:26
  • @ PhilS but i think @Matei Mihai answer is much better than that right answer – lazyCoder May 09 '17 at 07:46
  • Possible duplicate of [How to find first day of the next month and remaining days till this date with PHP](http://stackoverflow.com/questions/1777384/how-to-find-first-day-of-the-next-month-and-remaining-days-till-this-date-with-p) – Kaspar Kjeldsen May 09 '17 at 09:41

3 Answers3

6

You can use DateTime like:

$dateTime = new DateTime('2017-03-17');
$dateTime->modify('first day of next month');

echo $dateTime->format('Y-m-d');
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
0

Simply increment the date by 1 month and set the date to 1st of the month. Do -

date('Y-m-01', strtotime('+1 MONTH', strtotime($date)));

Working code

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

you can get it using

$date = '2017-03-17';
echo date('Y-m-01', strtotime('+1 month',strtotime($date)));

https://eval.in/790237

Nishant Nair
  • 1,999
  • 1
  • 13
  • 18