fork(1) download
  1. <?php
  2. /**
  3.  * Takes start and end date string in format 'mm/yyyy' along with a $months and $years
  4.  * arrays; modifies the arrays in place to add all months and years between the dates
  5.  */
  6. function listMonthsAndYears($start, $end, &$months, &$years) {
  7.  
  8. list($startM, $startY) = array_map('intval',explode('/',$start));
  9. list($endM, $endY) = array_map('intval',explode('/',$end));
  10. $m = $startM;
  11. $y = $startY;
  12.  
  13. while($endY > $y || ($endY === $y && $endM >= $m) ){
  14. $months[]= $m;
  15. $years[] = $y;
  16. $m++;
  17. if($m > 12){
  18. $m = 1;
  19. $y++;
  20. }
  21. }
  22. }
  23.  
  24.  
  25. $start = '2/2016';
  26. $end = '11/2017';
  27. listMonthsAndYears($start, $end, $months, $years);
  28.  
  29. print_r([$months,$years]);
  30.  
Success #stdin #stdout 0s 82880KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 4
            [3] => 5
            [4] => 6
            [5] => 7
            [6] => 8
            [7] => 9
            [8] => 10
            [9] => 11
            [10] => 12
            [11] => 1
            [12] => 2
            [13] => 3
            [14] => 4
            [15] => 5
            [16] => 6
            [17] => 7
            [18] => 8
            [19] => 9
            [20] => 10
            [21] => 11
        )

    [1] => Array
        (
            [0] => 2016
            [1] => 2016
            [2] => 2016
            [3] => 2016
            [4] => 2016
            [5] => 2016
            [6] => 2016
            [7] => 2016
            [8] => 2016
            [9] => 2016
            [10] => 2016
            [11] => 2017
            [12] => 2017
            [13] => 2017
            [14] => 2017
            [15] => 2017
            [16] => 2017
            [17] => 2017
            [18] => 2017
            [19] => 2017
            [20] => 2017
            [21] => 2017
        )

)