dongpao1905 2012-11-10 11:44
浏览 29
已采纳

这一刻()让我疯了

I know that this problem is really pitty and i just made a really big mistake what i cannot realise. I want to load an array with dates. I have the starting and the ending dates. Here is my code:

$year_from=2011;
$month_from=1;
$year_till=2012;
$month_till=5;
$array=array();
$year=$year_from;
$month=$month_till;
while($year!=$year_till and $month!=$month_till){
    $array[]=$year.'-'.$month;
    if($month==12){
        $month=1;
        $year=$year+1;
    }else{
        $month=$month+1;
    }
}
print_r($array);

This script's out put will be the following:

Array ( [0] => 2011-1 [1] => 2011-2 [2] => 2011-3 [3] => 2011-4 [4] => 2011-5 [5] => 2011-6 [6] => 2011-7 [7] => 2011-8 [8] => 2011-9 [9] => 2011-10 )

Please help i know it is a really simple task but it's driving me crazy. Any help is greatly appriciated.

EDIT:

Desired output:

Array ( [0] => 2011-1 [1] => 2011-2 [2] => 2011-3 [3] => 2011-4 [4] => 2011-5 [5] => 2011-6 [6] => 2011-7 [7] => 2011-8 [8] => 2011-9 [9] => 2011-10 [10] => 2011-11 [11] => 2011-12 [12] => 2012-1 [13] => 2012-2 [14] => 2012-3 [15] => 2012-4 )
  • 写回答

5条回答 默认 最新

  • douyi3632 2012-11-10 11:48
    关注
    while(!($year==$year_till and $month==$month_till)){
        $array[]=$year.'-'.$month;
        if($month==12){
            $month=1;
            $year=$year+1;
        }else{
            $month=$month+1;
        }
    }
    

    The condition is now true while it is false that both the month and the year are equal, which is when you have reached your target date.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图