2016-07-25 5 views
0

이 현재 배열 다음에 각 달에 대한 배열을 만드는 방법을 찾을 수 없습니다.오늘 이후의 각 달에 대한 배열 만들기

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

나는 온다 매월이

2016-09

2016-08

2016-07

같은 것을 보이는 드롭 다운리스트를 싶어. 시작일은 2016-07이어야합니다. 그리고 최고의 가치는 당월이어야합니다.

답변

4

그냥 그들을 통해 시작 및 종료 날짜와 루프를 만들 :이이 작업을 수행하는 가장 깨끗한 방법이며 DST을 처리하고 윤년 기본적으로

$start = (new DateTime('next month'))->modify('first day of this 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 as $dt) { 
    echo $dt->format("Y-m") . "<br>\n"; 
} 

내가 DateInterval()DatePeriod()DateTime()를 사용합니다. 나를 위해 일한

Demo

+0

덕분에, 난 그냥 이번 달로 고정되는 시작과 끝을 수정했습니다. 솔루션 주셔서 감사합니다 :) –

관련 문제