0

我知道这个问题真的很可怜,我只是犯了一个我无法意识到的大错误。我想加载一个带有日期的数组。我有开始日期和结束日期。这是我的代码:

$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);

该脚本的输出如下:

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 )

请帮助我知道这是一项非常简单的任务,但它让我发疯。非常感谢任何帮助。

编辑:

期望的输出:

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 )
4

5 回答 5

2
while(!($year==$year_till and $month==$month_till)){
    $array[]=$year.'-'.$month;
    if($month==12){
        $month=1;
        $year=$year+1;
    }else{
        $month=$month+1;
    }
}

现在条件为真,而月份和年份相等则为假,即您已到达目标日期

于 2012-11-10T11:48:03.567 回答
0

尝试:

$year_from=2011;
$month_from=1;
$year_till=2013;
$month_till=11;
$array=array();
$year=$year_from;
$month=$month_from;
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);
于 2012-11-10T11:51:38.437 回答
0

您可以运行两个 for 循环: 1. 外部 for 循环将持续 2011 年至 2013 年(包括)。2. 内部 for 循环将经过 1 到 12 个月(将它们重置为 12 到 1)。内部循环将检查年份是否等于 2013,然后在第 11 个月停止。

伪代码:

for (int year = 2011; year <= 2013; year++)
    for (int month = 1; month <= 12; month++)
    {
        //Add to array year and month
        //Check if year == 2013 && month == 11 the break loop
        //Check if month == 12 then month = 1
    }

我没有t recognize the language you use (I think it使用 PHP),但我确定它有处理 DateTime 的类,并且有将 +1 添加到月份的方法。在这种情况下,您使用 while 循环将 +1 添加到月份并检查我们是否达到了所需的日期。

于 2012-11-10T12:00:48.143 回答
0

我不确定你在逻辑上试图做什么,但这里是给出你想要的答案的代码:

<?php 

$year_from=2011;
$month_from=1;
$year_till=2013;
$month_till=13;
$array=array();
$i = 0;

while($year_from < $year_till){
  while($month_from < $month_till){
     $array[$i] = $year_from.'-'.$month_from;
     $i++;
      $month_from++;
   }
  $year_from++;    
  $month_from= 1;

}

print_r($array);

?>

这输出

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 [16] => 2012-5 [17] => 2012-6 [18] => 2012-7 [19] => 2012-8 [20] => 2012-9 [21] => 2012-10 [22] => 2012-11 [23] => 2012-12 ) 
于 2012-11-10T12:01:06.600 回答
0
$year_from=2011;
$month_from=1;
$year_till=2012;
$month_till=5;
$array=array();
$year=$year_from;
$month=$month_from;
echo "<br>".$year_till. " " .$month."<br>"; 
while($year!=$year_till || ($year==$year_till && $month!=$month_till)){
    $array[]=$year.'-'.$month;
    if($month==12){
        $month=1;
        $year=$year+1;
    }
    else{
        $month=$month+1;
    }
}
print_r($array);
于 2012-11-10T12:15:25.360 回答