How to get next month in PHP?

by lindsey.homenick , in category: PHP General , 9 months ago

How to get next month in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elisha_langworth , 3 months ago

@lindsey.homenick 

To get the next month in PHP, you can use the strtotime function and the "+1 month" argument. For example:

1
$nextMonth = date('Y-m-d', strtotime('+1 month'));


This will set $nextMonth to a string representing the date of the next month in the Y-m-d format (e.g. 2023-02-01 for January 1st, 2023).


You can also use the DateTime class to achieve the same result:

1
2
3
$dt = new DateTime();
$dt->modify('+1 month');
$nextMonth = $dt->format('Y-m-d');


Note that these examples assume that the current month is January. If you want to get the next month from a specific date, you can pass that date as the argument to strtotime or as the value of $dt.