Get next month using mktime()

1.1k views Asked by At

I'm trying to get the next month using PHP mktime() function.

I am looking to get a result like 07/2015 but instead it is giving me 01/2015.

Here is the code I am using:

$next_month = strftime('%d/%Y', strtotime('+1 month', mktime(0,0,0,$month,1,$year)));

The value of $month is 06.

The value of $year is 2015.

2

There are 2 answers

0
varnie On BEST ANSWER

If you insist on using your version, then it should be %m instead of %d, i.e.:

$year = 2015;
$month = 6;
echo strftime('%m/%Y', strtotime('+1 month', mktime(0,0,0,$month,1,$year)));
2
Jonnix On

How about giving DateTime a try. For example:

$date = new DateTime('next month');
echo $date->format('m/Y');

If the month and year are variable, then the following would also work:

$date = new DateTime("$year-$month-01");
$date->modify('next month');
echo $date->format('m/Y');