1
    $timein_out = $this->time_model->get_timein_out($this->input->get('i'));   

    $total_diff= array();
    $hours = array();
    $mins =array();

    foreach($timein_out as $timetest)
     {
        $total_diff[] = strtotime($timetest["Time_out"]) - strtotime($timetest["Time_in"]);

        for($key=0;$key<count($total_diff);$key++)
        {
            $hours[] = intval(floor($total_diff[$key]/3600));
            $mins[] =  intval(($total_diff[$key]-$hours[$key]*3600)/60); 
        }

     };

     echo json_encode($total_diff); // output: [33600,34560,35160]
     echo json_encode($hours); // [9,9,9,9,9,9]
     echo json_encode($mins); //[20,20,36,20,36,46]

The actual output should be

 echo json_encode($hours); // [9,9,9,]
 echo json_encode($mins); //[20,36,46]

Question: Why is the output repeating? what is the problem in my code? :( thanks.

1
  • what is the value of $timein_out? Commented Sep 23, 2013 at 3:21

2 Answers 2

3

may be you want the inner loop outside. try this.

 foreach($timein_out as $timetest)
 {
    $total_diff[] = strtotime($timetest["Time_out"]) - strtotime($timetest["Time_in"]);

 }
 for($key=0;$key<count($total_diff);$key++)
 {
     $hours[] = intval(floor($total_diff[$key]/3600));
     $mins[] =  intval(($total_diff[$key]-$hours[$key]*3600)/60); 
 }
Sign up to request clarification or add additional context in comments.

2 Comments

+1 better approach than the accepted answer.its run for 2n times and accepted answer runs for n*n times....
yeah.. i think this one is faster than the other one.. :)
1

may be try unset()'ting the $total_diff like:

foreach($timein_out as $timetest) {
    $total_diff[] = strtotime($timetest["Time_out"]) - strtotime($timetest["Time_in"]);

    for($key=0;$key<count($total_diff);$key++) {
        $hours[] = intval(floor($total_diff[$key]/3600));
        $mins[] =  intval(($total_diff[$key]-$hours[$key]*3600)/60); 
    }
    unset($total_diff);
}

1 Comment

sorry dude i think the other one is much faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.