这一刻()让我疯了

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

Try:

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

You can run two for loops: 1. the outer for loop will go over the years 2011 till 2013 (including). 2. the inner for loop will go over the months 1 till 12 (reseting them at 12 to 1). the inner loop will check if year equals 2013 then stop at month 11.

pseudo code:

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
    }

I didnt recognize the language you use (I think its PHP) but I`m sure it has classes handling DateTime and has methods to add +1 to the month. In this case you case use a while loop which adds +1 to the month and checks if we reached the required date.

I am not sure what you were trying to do logically but here is the code which out puts your desired answer:

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

?>

this outputs

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