senzacionale
senzacionale

Reputation: 20906

how to get next month using timestamp in php using time() function

i can get current timestamp with time(); How can i add him and get next month? So how can i get next month with timestamp?

I try with mktime and strtotime but none works.

Example:

$date = time();
$month = date('m', $date);

how to get next mont?

Upvotes: 1

Views: 5819

Answers (4)

phihag
phihag

Reputation: 287755

$month = date('n') % 12 + 1;

Upvotes: 2

Emil Vikström
Emil Vikström

Reputation: 91902

If you just add one month, you'll end up skipping months now and then. For example, what is 31th of May plus one month? Is it the last of June or is it the first of July? In PHP, strtotime will take the latter approach and give you the first day of July.

If you just want to know the month number, this is a simple approach:

$month = date('n') + 1;
if($month > 12) {
  $month = $month % 12;
}

or for infinite flexibility (if you need to configure how many months to add or subtract):

$add = 1;
$month = ((date('n') - 1 + $add) % 12) + 1;

Upvotes: 4

Bjoern
Bjoern

Reputation: 16304

This gives you a date in next month:

 $plusonemonth = date("Y-m-d",strtotime("+1 months"));

Modifying the output format to just m gives you the next months number.

Upvotes: 3

David Fells
David Fells

Reputation: 6798

$month = date('m', strtotime('+1 months'));

Upvotes: 0

Related Questions