duanjie9630
duanjie9630
2015-06-23 11:54
浏览 87
已采纳

下个月使用mktime()获取

I'm trying to get the next month using PHP mktime() function.

I am looking to get a result like 07/2015 but instead it is giving me 01/2015.

Here is the code I am using:

$next_month = strftime('%d/%Y', strtotime('+1 month', mktime(0,0,0,$month,1,$year)));

The value of $month is 06.

The value of $year is 2015.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用PHP mktime()函数来获得下个月。< / p>

我希望得到像 07/2015 这样的结果,但它却给了我 01/2015 \ n

以下是我正在使用的代码:

  $ next_month = strftime('%d /%Y',strtotime('+ 1 month',mktime  (0,0,0,$ month,1,$ year))); 
   
 
 

$ month 的值为06。

$ year 的值是2015年。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhuan0309
    douzhuan0309 2015-06-23 12:00
    已采纳

    If you insist on using your version, then it should be %m instead of %d, i.e.:

    $year = 2015;
    $month = 6;
    echo strftime('%m/%Y', strtotime('+1 month', mktime(0,0,0,$month,1,$year)));
    
    点赞 评论
  • dragon7713
    dragon7713 2015-06-23 11:58

    How about giving DateTime a try. For example:

    $date = new DateTime('next month');
    echo $date->format('m/Y');
    

    If the month and year are variable, then the following would also work:

    $date = new DateTime("$year-$month-01");
    $date->modify('next month');
    echo $date->format('m/Y');
    
    点赞 评论

相关推荐