Metzed
Metzed

Reputation: 491

How can I get name of month after the current month?

The following gives me October

 echo date('F');

I'm using the following code to give me the name of the coming month (the month after this one), so I'm hoping to see November.

$nextmonth = date("F",strtotime("+1 months"));  

Today is 31st Oct, but the above gives 'December' What am I doing wrong? How can I get the month after the current month?

Upvotes: 0

Views: 77

Answers (1)

John Conde
John Conde

Reputation: 219804

Use relative formats to get the first day of the next month:

echo (new DateTime('first day of next month'))->format('F');

The reason why you see this problem is when adding time to a date at the end of the month you run into issues with months having fewer than 31 days. This can cause you to skip a month. The best bet is to always start your date math at the beginning of the month before adding time to it or just relative formats as demonstrated above.

PHP 5.3:

$nextMonth = new DateTime('first day of next month');
echo $nextMonth->format('F');

Upvotes: 3

Related Questions