how to calculate total working hours for all users?
<p>I have a code that takes two arrays :</p>

<p><strong>Login array</strong></p>

<p><strong>Logout Array</strong></p>

<p>and then my code finds difference between one login and logout, so that would be his working duration in office. So will calculate all the duration and will get total worked hours for a single user for each date. of month.</p>

<p>Now this does for only one user . How can i calculate every user's total duration  and print it in view?</p>

<pre><code>&lt;?php
                $loginarry = $attendances_logins;
                $logoutarry =$attendances_logouts;
                $timeduration=0;
                $login_i=0;
                $logout_i=0;


                echo '&lt;table border="1px"  class="table table-striped table-bordered table-hover" id="dataTables-example"&gt;';
                echo '&lt;tr&gt;&lt;th&gt;Login&lt;/th&gt;&lt;th&gt;Logout&lt;/th&gt;&lt;th&gt;Duration&lt;/th&gt;&lt;/tr&gt;';

                //echo ("Array Length = " . count($loginarry)." &amp; ".count($logoutarry)."\n&lt;br/&gt;");
        while(true)
        { 
            if( $login_i &gt;= count($loginarry) &amp;&amp; $logout_i &gt;= count($logoutarry))
                break;

            if( $logout_i &gt;= count($logoutarry))
            {
                //echo ("Login[".$login_i."] : ".$loginarry[$login_i]-&gt;date_data." and Logout : -----------------\n&lt;br/&gt;");


echo ("&lt;tr&gt;&lt;td&gt;".$loginarry[$login_i]-&gt;date_data." &lt;/td&gt;&lt;td&gt;Not Available&lt;/td&gt;&lt;td&gt;Not Available&lt;/td&gt;&lt;/tr&gt;");


    //          echo ("&lt;tr&gt; &lt;td&gt;".$loginarry-&gt;date_data." &lt;/td&gt;&lt;td&gt; Data Not Available&lt;/td&gt;&lt;/tr&gt;");
                $login_i++;
                continue;
            }

            if(($login_i+1) == count($loginarry) &amp;&amp; ($logout_i+1) &lt; count($logoutarry))
            {
                echo ("&lt;tr&gt;&lt;td&gt;Data Not Available &lt;/td&gt;&lt;td&gt;".$logoutarry[$logout_i]-&gt;date_data."\n&lt;/td&gt;&lt;td&gt;Data Not Available&lt;/td&gt;&lt;/tr&gt;");
            $logout_i++;
            continue;
            }

            if( $login_i &gt;= count($loginarry))
            {
                echo ("&lt;tr&gt;&lt;td&gt; Data Not Available &lt;/td&gt;&lt;td&gt;".$logoutarry[$logout_i]-&gt;date_data."\n&lt;td&gt;Data Not Available&lt;/td&gt;&lt;/td&gt;&lt;/tr&gt;");
                            $logout_i++;
                continue;
            }
            //echo( "******* ".(new DateTime($loginarry[$login_i+1]-&gt;date_data))-&gt;format('U') . " **** ".(new DateTime($logoutarry[$logout_i]-&gt;date_data))-&gt;format('U'). "\n&lt;br/&gt;");

            //check if next login time is smaller then current logout time - if so skip current login time
            if($login_i &lt; (count($loginarry)-1) &amp;&amp; (new DateTime($logoutarry[$logout_i]-&gt;date_data))-&gt;format('U') &gt; (new DateTime($loginarry[$login_i+1]-&gt;date_data))-&gt;format('U'))
            //if( date_diff(date_create($logoutarry[$logout_i]-&gt;date_data), date_create($loginarry[$login_i+1]-&gt;date_data)) &gt; 0 )
            {
                echo ("&lt;tr&gt;&lt;td&gt;".$loginarry[$login_i]-&gt;date_data."&lt;/td&gt;&lt;td&gt;Data Not Available&lt;/td&gt;&lt;td&gt;Data Not Available&lt;/td&gt;&lt;/tr&gt;");
                $login_i++;
                continue;
            }

           //
            if( (new DateTime($loginarry[$login_i]-&gt;date_data))-&gt;format('U') &gt; (new DateTime($logoutarry[$logout_i]-&gt;date_data))-&gt;format('U') )
            //if( date_diff(date_create($loginarry[$login_i]-&gt;date_data), date_create($logoutarry[$logout_i]-&gt;date_data)) &gt; 0 )
                    {
                echo ("&lt;tr&gt;&lt;td&gt;Data Not Available &lt;/td&gt;&lt;td&gt;".$logoutarry[$logout_i]-&gt;date_data."\n&lt;/td&gt;&lt;td&gt;Data Not Available&lt;/td&gt;&lt;/tr&gt;");

                            $logout_i++;
                            continue;
                    }

            //if more logout entries then skip to last logout entry
            if( $login_i &lt; (count($loginarry)-1) &amp;&amp; $logout_i &lt; (count($logoutarry)-1) &amp;&amp; (new DateTime($logoutarry[$logout_i+1]-&gt;date_data))-&gt;format('U') &lt; (new DateTime($loginarry[$login_i+1]-&gt;date_data))-&gt;format('U'))
            {
                echo(" &lt;tr&gt;&lt;td&gt; Data Not Available&lt;/td&gt; &lt;td&gt;".$logoutarry[$logout_i]-&gt;date_data."\n&lt;/td&gt; &lt;td&gt;Data Not Available&lt;/td&gt; &lt;/tr&gt;");
                            $logout_i++;
                            continue;
            }

            echo ("&lt;tr&gt;&lt;td&gt; ".$loginarry[$login_i]-&gt;date_data."&lt;/td&gt;  &lt;td&gt;".$logoutarry[$logout_i]-&gt;date_data."&lt;/td&gt;");




            $singleduration = ((new DateTime($logoutarry[$logout_i]-&gt;date_data))-&gt;format('U') - (new DateTime($loginarry[$login_i]-&gt;date_data))-&gt;format('U'));
            if($singleduration &gt; 43200)//12 hours X 3600 seconds)
            {   
                 //$timeduration += $singleduration;
                 echo ("&lt;td class='danger'&gt;Huge duration - not considered - please contact HR (".$singleduration.")&lt;/td&gt;&lt;/tr&gt;");
            }
            else
            {
                 $timeduration += $singleduration;
                 echo ("&lt;td&gt;".gmdate("H:i:s",$singleduration)."&lt;/td&gt;&lt;/tr&gt;");
            }
            $login_i++;
            $logout_i++;

        }

            echo ("&lt;/table&gt;");

            //$finaltime=gmdate("H:i:s",$timeduration);
            //$timedurationstr = ConvertSecondsToStr($timeduration);
            $hours = 0;
            if($timeduration &gt; 3600)
            {
                $hours = floor($timeduration/3600);
                $timeduration -= (3600*$hours);
            }
            $min = 0;
            if($timeduration &gt; 60)
            {
                $min = floor($timeduration/60);
                $timeduration -= (60*$min);
            }
            $seconds = $timeduration;



           //echo("&lt;div class= 'row'&gt; &lt;div class ='col-sm-4' style= 'float:right'&gt;&lt;h3 class='alert alert-success'&gt;&lt;center&gt;Total Hours Worked ". $finaltime." Hours&lt;/center&gt;&lt;/h3&gt;&lt;/div&gt;&lt;/div&gt;");
           echo("&lt;div class= 'row'&gt; &lt;div class ='col-sm-4' style= 'float:right'&gt;&lt;h3 class='alert alert-success'&gt;&lt;center&gt;Total time worked: ". $hours." Hour(s) ".$min." Minute(s) ".$seconds." Second(s)&lt;/center&gt;&lt;/h3&gt;&lt;/div&gt;&lt;/div&gt;");
           //echo("&lt;div class= 'row'&gt; &lt;div class ='col-sm-4' style= 'float:right'&gt;&lt;h3 class='alert alert-success'&gt;&lt;center&gt;Total time worked: ". $timedurationstr." &lt;/center&gt;&lt;/h3&gt;&lt;/div&gt;&lt;/div&gt;");

                ?&gt;
</code></pre>

<p>the Model code:</p>

<pre><code> public function all_login($emp_id)
{

    $startDate = new DateTime("first day of last month");
    $endDate = new DateTime("last day of last month");
    $this-&gt;db-&gt;select('*');
    $this-&gt;db-&gt;from('daily_data2');
    $this-&gt;db-&gt;where('users.emp_id',$emp_id);
    $this-&gt;db-&gt;where('entry &gt;','100');
    $this-&gt;db-&gt;where("date(date_data) BETWEEN '" . $startDate-&gt;format( 'Y-m-d' ) . "' AND '" . $endDate-&gt;format( 'Y-m-d') . "' ");                                                  
    $this-&gt;db-&gt;order_by("date_data","ASC");
    $this-&gt;db-&gt;join('users', 'users.emp_id = daily_data2.emp_id','inner');     
    $query = $this-&gt;db-&gt;get(); 
    $res   = $query-&gt;result();        
    return $res;

}




public function all_logout($emp_id)
{


      $startDate = new DateTime("first day of last month");
      $endDate = new DateTime("last day of last month");
      $this-&gt;db-&gt;select('*');
      $this-&gt;db-&gt;from('daily_data2');
      $this-&gt;db-&gt;where('users.emp_id',$emp_id);
      $this-&gt;db-&gt;where('entry &lt;','100');
      $this-&gt;db-&gt;where("date(date_data) BETWEEN '" . $startDate-&gt;format( 'Y-m-d' ) . "' AND '" . $endDate-&gt;format( 'Y-m-d') . "' ");                                                     
      $this-&gt;db-&gt;order_by("date_data","ASC");
      $this-&gt;db-&gt;join('users', 'users.emp_id = daily_data2.emp_id','inner');
      $query = $this-&gt;db-&gt;get(); 
      $res   = $query-&gt;result();        
      return $res;


}
</code></pre>

<p>The Arrays:</p>

<pre><code>          array (size=21)
        0 =&gt; 
          object(stdClass)[27]
            public 'id' =&gt; string '1' (length=1)
            public 'emp_id' =&gt; string '1' (length=1)
            public 'name' =&gt; string 'Hiren' (length=5)
            public 'last_name' =&gt; string 'Gada' (length=4)
            public 'user_type' =&gt; string 'employee' (length=8)
            public 'email' =&gt; string 'hiren@bizrtc.com' (length=16)
            public 'password' =&gt; string '55f32a0322e3b2f1bf8f41e20bd1dd075f50cfc2becc4fd9bc85ac10eea4bf0c5be3b00596f1d8b4fda6e5ab1c3c388bc28fdc0454bc260382be86613057f045' (length=128)
            public 'phone' =&gt; string '9999999999' (length=10)
            public 'gender' =&gt; string 'Male' (length=4)
            public 'designation' =&gt; string 'CEO' (length=3)
            public 'blood_group' =&gt; string '+ve' (length=3)
            public 'date_birth' =&gt; string 'DD-MM-YYYY' (length=10)
            public 'status' =&gt; string 'active' (length=6)
            public 'address' =&gt; string 'DD-MM-YYYY' (length=10)
        1 =&gt; 




    object(stdClass)[310]
            public 'id' =&gt; string '2' (length=1)
            public 'emp_id' =&gt; string '3' (length=1)
            public 'date_data' =&gt; string '2015-12-01 18:20:15' (length=19)
            public 'abc' =&gt; string '1' (length=1)
            public 'def' =&gt; string '0' (length=1)
            public 'entry' =&gt; string '101' (length=3)
            public 'ghi' =&gt; string '0' (length=1)
            public 'status' =&gt; string 'active' (length=6)
            public 'name' =&gt; string 'Deepa' (length=5)
            public 'last_name' =&gt; string 'Gada' (length=4)
            public 'user_type' =&gt; string 'employee' (length=8)
            public 'email' =&gt; string 'deepa@bizrtc.com' (length=16)
            public 'password' =&gt; string '108fae131090e2cd93d569050a105f5187a3a9c2e9fef0010f4acea30d999834d4c36cd0435550a42b8c98f9a7dcbfd05cb9b1428aa596a71b44e20879e233bb' (length=128)
            public 'phone' =&gt; string '9999999999' (length=10)
            public 'gender' =&gt; string 'Female' (length=6)
            public 'designation' =&gt; string 'Sr. Software' (length=12)
            public 'blood_group' =&gt; string '+ve' (length=3)
            public 'date_birth' =&gt; string 'DD-MM-YYYY' (length=10)
            public 'address' =&gt; string 'DD-MM-YYYY' (length=10)




              object(stdClass)[1142]
            public 'id' =&gt; string '20' (length=2)
            public 'emp_id' =&gt; string '22' (length=2)
            public 'date_data' =&gt; string '2015-12-11 07:34:22' (length=19)
            public 'abc' =&gt; string '1' (length=1)
            public 'def' =&gt; string '0' (length=1)
            public 'entry' =&gt; string '101' (length=3)
            public 'ghi' =&gt; string '0' (length=1)
            public 'status' =&gt; string 'active' (length=6)
            public 'name' =&gt; string 'Parveen' (length=7)
            public 'last_name' =&gt; string 'Thakur' (length=6)
            public 'user_type' =&gt; string 'employee' (length=8)
            public 'email' =&gt; string 'praveen@bizrtc.com' (length=18)
            public 'password' =&gt; string '3eee66dbace42d2e671c52013e41de441b176dbaa0f7df33a5811b86c78b60ecb5328184bf1f5057f94817801140d7287f31c1fb06fa65550c356a33a8eec0db' (length=128)
            public 'phone' =&gt; string '9999999999' (length=10)
            public 'gender' =&gt; string 'Male' (length=4)
            public 'designation' =&gt; string 'Support Staff' (length=13)
            public 'blood_group' =&gt; string '+ve' (length=3)
            public 'date_birth' =&gt; string 'DD-MM-YYYY' (length=10)
            public 'address' =&gt; string 'DD-MM-YYYY' (length=10)
</code></pre>