douyunhuan9886 2017-06-01 06:12
浏览 23
已采纳

根据给定的月份和年份,获得前三个月的年份

Is there any way to get the previous three months if I am giving the year and month as input (in PHP). Suppose I am giving the values,

$month = "2";
$year = 2016;

It should return like below array,

Array ( [0] => '11 2015' [1] => '12 2015' [2] => '01 2016')
  • 写回答

4条回答 默认 最新

  • douliangpo0128 2017-06-01 06:35
    关注

    Use mktime

    $month = "2";
    $year = 2016;
    
    for($i=3;$i>0;$i--){
        $arr[]=date("m-Y", mktime(0, 0, 0, $month-$i, 01, $year));
    
    
    }
    print_r($arr);
    

    output

    Array ( [0] => 11-2015 [1] => 12-2015 [2] => 01-2016 )
    

    EDIT for your comment replace m with n

    $arr[]=date("n-Y", mktime(0, 0, 0, $month-$i, 01, $year));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?