1

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?

senzacionale
  • 20,448
  • 67
  • 204
  • 316
  • possible duplicate of [php date format YYYY-MM-DD minus or add one week from now ?](http://stackoverflow.com/questions/6086389/php-date-format-yyyy-mm-dd-minus-or-add-one-week-from-now) – JohnP May 22 '11 at 09:51
  • 3
    JohnP, it's easier to add/subtract weeks than months since month calculations needs a definition of what "31th plus one month" should be. – Emil Vikström May 22 '11 at 09:58

4 Answers4

4

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;
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
3

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.

Bjoern
  • 15,934
  • 4
  • 43
  • 48
2
$month = date('n') % 12 + 1;

phihag
  • 278,196
  • 72
  • 453
  • 469
0
$month = date('m', strtotime('+1 months'));
David Fells
  • 6,678
  • 1
  • 22
  • 34
  • @senzacionale If you have another question, please ask it by clicking the [Ask Question](http://stackoverflow.com/questions/ask) button. – Dustin Aug 09 '12 at 14:34