Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the next month in PHP

Tags:

php

I need to get the next month with php. Everywhere is written these example

date('Y-m-t', strtotime('+1 month'));

The output of the above code is '2017-03-31'. I need to get February, not March.

like image 923
user3351236 Avatar asked Jan 31 '17 07:01

user3351236


3 Answers

If you want to get the next irrespective of the current date in the current month. below code may help you

echo date('M',strtotime('first day of +1 month'));
// e.g. "Jan"

echo date('m',strtotime('first day of +1 month'));
// e.g. "1"

echo date('F',strtotime('first day of +1 month'));
// e.g. "January"

This will give you Next month.

You can find more formatting masks in date() function documentation

like image 58
Azeez Kallayi Avatar answered Sep 23 '22 07:09

Azeez Kallayi


To get next month using php use string to time function as below:

$nxtm = strtotime("next month");
echo date("M", $nxtm);
like image 45
Aarsh Avatar answered Sep 24 '22 07:09

Aarsh


Use Like This

// One month from today
$date = date('Y-m-d', strtotime('+1 month'));

// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));
like image 31
Indresh Tayal Avatar answered Sep 25 '22 07:09

Indresh Tayal