以适当的方式改变循环


Change loop in proper way

我有一个面试时间安排的代码。面试时间是上午9点到下午4点。但在下午1点到2点的休息时间。我正在使用for循环并检查2个条件。

控制器

   function appointment_schedule() 
   { 
    $start_date = $this->input->post('start_date'); 
    $filtered_students = $this->home_model->getFilterStudents(); 
    $fil_std_count = $filtered_students->num_rows(); 
    $filtered_student_ids = $this->home_model->getFilterStudentsIds(); 
    $st_time = strtotime("09:00 am"); 
    for ($i = 0; $i < $fil_std_count; $i++) 
    { 
        $end_time = date("H:i:s a", strtotime('+20 minutes', $st_time)); 
        if (strtotime($end_time) > (strtotime("01:00 pm"))) 
        { 
            $st_time = strtotime("02:00 pm"); 
            $end_time = date("H:i:s a", strtotime('+20 minutes', $st_time)); 
            $filtered_students = $this->home_model->insert_appointment_schedule(date("H:i:s a", ($st_time)), $end_time, $filtered_student_ids[$i]->applicant_id, $start_date); 
            $st_time = strtotime($end_time); 
        } 
        else  if (strtotime($end_time) >= strtotime("04:00 pm"))
            { 
                $start_date=date('Y-m-d', strtotime($start_date. ' + 1 days'));
                $st_time = strtotime("09:00 am"); 
                $end_time = date("H:i:s a", strtotime('+20 minutes', $st_time)); 
                $filtered_students = $this->home_model->insert_appointment_schedule(date("H:i:s a", ($st_time)), $end_time, $filtered_student_ids[$i]->applicant_id, $start_date); 
                $st_time = strtotime($end_time); 
            } 
            $filtered_students = $this->home_model->insert_appointment_schedule(date("H:i:s a", ($st_time)), $end_time, $filtered_student_ids[$i]->applicant_id, $start_date); 
        $st_time = strtotime($end_time); 
    }
}

但是有些循环问题,到达1 pm时检查满足的条件,然后执行,然后输出if条件。

但是它进入了第一个if条件然后重复执行这个条件到达for循环条件

请尝试此代码

function appointment_schedule() 
{ 
    $start_date = $this->input->post('start_date'); 
    $filtered_students = $this->home_model->getFilterStudents(); 
    $fil_std_count = $filtered_students->num_rows(); 
    $filtered_student_ids = $this->home_model->getFilterStudentsIds(); 
    $st_time = strtotime("09:00 am"); 
    for ($i = 0; $i < $fil_std_count; $i++) 
    { 
        $end_time = date("H:i:s a", strtotime('+20 minutes', $st_time)); 
        if (strtotime($end_time) >= (strtotime("01:00 pm")&&strtotime($end_time) < (strtotime("01:19 pm"))) 
        { 
            $st_time = strtotime("02:00 pm"); 
            $end_time = date("H:i:s a", strtotime('+20 minutes', $st_time)); 
        } 
        else  if (strtotime($end_time) >= strtotime("04:00 pm"))
        { 
            $st_time = strtotime("09:00 am"); 
            $end_time = date("H:i:s a", strtotime('+20 minutes', $st_time)); 
            $start_date=date('Y-m-d', strtotime($start_date. ' + 1 days'));
        } 
        $filtered_students = $this->home_model->insert_appointment_schedule(date("H:i:s a", ($st_time)), $end_time, $filtered_student_ids[$i]->applicant_id, $start_date); 
        $st_time = strtotime($end_time); 
    }
}