V4n1ll4
V4n1ll4

Reputation: 6099

Get next month using mktime()

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.

Upvotes: 1

Views: 1176

Answers (2)

Jonnix
Jonnix

Reputation: 4187

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');

Upvotes: 0

varnie
varnie

Reputation: 2595

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)));

Upvotes: 2

Related Questions