执行时间超过了PHP Carbon

时间:2017-12-01 22:24:10

标签: php

我有这个功能来检查开始,结束和重复日期。因此,开始日期和结束日期用作日期范围,repeatFollowup是在该时间段内每个月重复的日期。我无法调试代码导致延迟或是无限循环?

感谢任何帮助。谢谢



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;

        }
}
&#13;
&#13;
&#13;

1 个答案:

答案 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);
        }