doutang1884 2015-12-15 21:17
浏览 10

在数组中添加数月

Hi I've created an array where I increment "25/11/2015" by 1 month. I have a question about how it is working. I'm currently using "strtotime('+1 month', $currentdate)". I was wondering why in my first row of the array +1month isn't added straight away. Instead I am left with NOV-15 which is what I want but I was just wondering why this happens.

currently I'm getting:

Nov-15
Dec-15
Jan-16

why am i not outputting this result:

Dec-15 Jan-16 Feb-16

Heres my code:

$date = "2015-11-25";
$start = strtotime($date);

$currentdate = $start;

 $times_table = array();
        for($i = 0; $i <= 3; $i++){
            $times_table[$i] = array();

        }
echo "<pre>";

       for($i = 0; $i <= 3; $i++){
             for($j = 0; $j <= 4; $j++){

               if ($j == 0){
                $times_table[$i][$j]=  "Version 5" ;
            }
                else if ($j == 1){
                $cur_date = date("M-y", $currentdate);

                $currentdate = strtotime('+1 month', $currentdate);

                $times_table[$i][$j]= $cur_date ;

          echo  $cur_date . ">". "<br />";
                }
                else{
                    $times_table[$i][$j]=  "gary" ;
                }
                if ($j == 3) {
                    $numbers = mt_rand(1, 100);
                    $times_table[$i][$j]= $numbers ;

                }
                if ($j == 4){

                    if($i == 0 || $i == 3)
                    {
                        $pay = "P";

                    $times_table[$i][$j]= $pay ;
                    }
                    else{
                        $int = "I";

                    $times_table[$i][$j]= $int ;

                    }
                }

            }

            }
  • 写回答

1条回答 默认 最新

  • douji2283 2015-12-15 21:58
    关注

    What you write is what you get:

    $cur_date = date("M-y", $currentdate);
    $currentdate = strtotime('+1 month', $currentdate);
    $times_table[$i][$j]= $cur_date ;
    

    You set the $currentdate value to $cur_date before one month was added, and then store the $cur_date value. This is the reason why it doesnt add 1 month straight away (it is added straight away to $currentdate but not to $cur_date)

    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。