Reputation: 78
Here is my code for getting next month value i.e 12 but this output 01
<?php
$month = '11';
$month = Date("m", strtotime($month. " +1 month"));
echo $month;
?>
i also tried this
<?php
$month = '11';
$month = strtotime("+1 month", $month);
echo $month;
?>
and got this result
2678411
Please help
Upvotes: 0
Views: 4000
Reputation: 2958
With strtotime()
:
$nextmonth = date('n', strtotime('2000-' . $month . '-01 + 1 month')); // returns string
Without strtotime()
:
$nextmonth = $month % 12 + 1; // returns integer
Upvotes: 2
Reputation: 314
$month = 1;
echo $nextMonth = $month === 12 ? 1 : ++$month;
Or simply for just next month using date and strtotime
echo date('m', strtotime('+1 month'));
Upvotes: 2
Reputation: 23958
If you want to use date() and strtotime() you can add a complete date to the month it works.
$month = '11';
$month = Date("m", strtotime("2017-" . $month . "-01" . " +1 month"));
echo $month;
This code will work for any input month and any year in the future, unless the calendar changes completely some day.
Whatever month you input it will output next month number
Upvotes: 5
Reputation: 46602
I would use date_create() like this 1 liner:
<?php
echo date_create()->modify('+1 month')->format('m');
?>
Upvotes: 2