如何使用time()函数在php中使用timestamp获取下个月

如何使用time()函数在php中使用timestamp获取下个月

问题描述:

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?

我可以通过time()得到当前时间戳; 我怎么能加他下个月? 那么我怎样才能在下个月获得时间戳? p>

我尝试使用mktime和strtotime但没有效果。 p>

示例: p>

  $ date = time(); 
 $ month = date('m',$ date); 
  code>  pre> 
 
 

如何获取 下一个mont? p> div>

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;

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

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

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.