php - Creating an array for each month after today -


i cant find way create array each month after current one.

            <?php                  $month = 07;                  $year = 2016;                 $end = date('y');                 for($i=0;$i<=$end-$year;$i++){                     $from = 1;                     if($i==0)                         $from = $month;                       for($y=$from;$y<=12;$y++){                         if($year==date('y') && $y > date('m'))                             break;                          $a = $year+$i.'-'.$y;                         $months[$a] = $a;                     }                 }                 krsort($months);             ?>             <?php echo html::dropdownlist('month_year',$month_year,$months,array('class'=>'form-control'));?> 

i want dropdownlist looks this

2016-09

2016-08

2016-07

for each month comes. starting date should 2016-07. , top value should current month.

just create start , end date , loop through them:

$start    = (new datetime('next month'))->modify('first day of month'); $end      = (new datetime('+24 months'))->modify('first day of next month'); $interval = dateinterval::createfromdatestring('1 month'); $period   = new dateperiod($start, $interval, $end);  foreach ($period $dt) {     echo $dt->format("y-m") . "<br>\n"; } 

i use datetime() dateinterval() , dateperiod() cleanest way , handles dst , leap year natively.

demo


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -