Reputation: 524
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
Upvotes: 1
Views: 1342
Reputation: 1997
you can get it using
$date = '2017-03-17';
echo date('Y-m-01', strtotime('+1 month',strtotime($date)));
Upvotes: 0
Reputation: 24276
You can use DateTime like:
$dateTime = new DateTime('2017-03-17');
$dateTime->modify('first day of next month');
echo $dateTime->format('Y-m-d');
Upvotes: 6
Reputation: 31749
Simply increment the date by 1
month and set the date to 1
st of the month. Do -
date('Y-m-01', strtotime('+1 MONTH', strtotime($date)));
Upvotes: 0