以适当的方式改变循环

时间:2015-10-10 16:57:06

标签: php codeigniter if-statement for-loop

我在面试中有一个时间安排的代码。访问时间是上午9点到下午4点。但是在下午1点到下午2点之间休息.I' m使用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点然后检查满足的条件然后执行那个..然后输出if条件。

但进入第一个if条件然后重复执行该条件达到for循环条件。

1 个答案:

答案 0 :(得分:0)

请尝试此代码

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