2017-12-01 2 views
0

이 함수는 시작, 끝 및 반복 날짜를 검사합니다. 따라서 시작 날짜와 종료 날짜는 날짜 범위로 사용되며 repeatFollowup은 해당 기간 동안 매월 반복되는 날짜입니다. 지연을 일으키는 원인이되는 코드를 디버그 할 수 없거나 무한 루프입니까?실행 시간 초과 PHP Carbon

도움을 주시면 감사하겠습니다. 당신은

public function calculateDaysOfMonth($startDate, $endDate, $repeatFollowup){ 
 

 
     $begin = new \DateTime($startDate); 
 
     $end = new \DateTime($endDate); 
 
     
 
       $repeatDate = new \DateTime($begin->format('Y-m').'-'.date($repeatFollowup)); 
 

 

 
     $days = array(); 
 

 
     
 

 
     elseif ($repeatFollowup==30){ 
 

 
      $newDate = Carbon::parse('first day of'.$repeatDate->format('Y-m-d')); 
 

 
      //var_dump($newDate->format('Y'));die(); 
 
      while($repeatDate<=$end){ 
 

 

 
       if($repeatDate->format('m')==2){ 
 
        $days[] = $newDate->addDays(27); 
 
        $newDate->modify('first day of next month') 
 
       } 
 
       else{ 
 
        $days[] = $newDate->addDays(29); 
 
        $newDate->modify('first day of next month') 
 
       } 
 

 

 
      } 
 

 
      var_dump($days);die(); 
 
      return $days; 
 

 
     } 
 
}

+1

'$ repeatDate'또는 '$ end'을 루프 내부에서 변경하지 않으므로 자연스럽게 끝나지 않습니다. –

+0

도움을 주셔서 감사합니다! 나는 별다른 문제가 없다는 것을 알 수있다. 그것은 실제로 지정된 날짜를 추가하는 것이 아니라 매월 첫날을 출력하는 것이다. –

답변

1

예 당신은 결코 당신의 repeatDate에 추가되지 않기 때문에 말보다는 (경우에 그게 전부 경우) 항상 낮은 것입니다 있도록, 무한 루프에, 당신은해야 할 감사합니다

 while($repeatDate<=$end){ 


      if($repeatDate->format('m')==2){ 
       $days[] = $newDate->addDays(27); 
       $newDate->modify('first day of next month') 
      } 
      else{ 
       $days[] = $newDate->addDays(29); 
       $newDate->modify('first day of next month') 
      } 
      $repeatDate->addDays(x); 
     }