我有一个带两个数组的代码:
登录数组
退出数组
然后我的代码发现一次登录和注销之间的区别,这就是他在办公室的工作时间。因此,将计算所有持续时间,并将获得每个日期的单个用户的总工作时间。月份。
现在这只适用于一个用户。如何计算每个用户的总持续时间并在视图中打印?
<?php
$loginarry = $attendances_logins;
$logoutarry =$attendances_logouts;
$timeduration=0;
$login_i=0;
$logout_i=0;
echo '<table border="1px" class="table table-striped table-bordered table-hover" id="dataTables-example">';
echo '<tr><th>Login</th><th>Logout</th><th>Duration</th></tr>';
//echo ("Array Length = " . count($loginarry)." & ".count($logoutarry)."\n<br/>");
while(true)
{
if( $login_i >= count($loginarry) && $logout_i >= count($logoutarry))
break;
if( $logout_i >= count($logoutarry))
{
//echo ("Login[".$login_i."] : ".$loginarry[$login_i]->date_data." and Logout : -----------------\n<br/>");
echo ("<tr><td>".$loginarry[$login_i]->date_data." </td><td>Not Available</td><td>Not Available</td></tr>");
// echo ("<tr> <td>".$loginarry->date_data." </td><td> Data Not Available</td></tr>");
$login_i++;
continue;
}
if(($login_i+1) == count($loginarry) && ($logout_i+1) < count($logoutarry))
{
echo ("<tr><td>Data Not Available </td><td>".$logoutarry[$logout_i]->date_data."\n</td><td>Data Not Available</td></tr>");
$logout_i++;
continue;
}
if( $login_i >= count($loginarry))
{
echo ("<tr><td> Data Not Available </td><td>".$logoutarry[$logout_i]->date_data."\n<td>Data Not Available</td></td></tr>");
$logout_i++;
continue;
}
//echo( "******* ".(new DateTime($loginarry[$login_i+1]->date_data))->format('U') . " **** ".(new DateTime($logoutarry[$logout_i]->date_data))->format('U'). "\n<br/>");
//check if next login time is smaller then current logout time - if so skip current login time
if($login_i < (count($loginarry)-1) && (new DateTime($logoutarry[$logout_i]->date_data))->format('U') > (new DateTime($loginarry[$login_i+1]->date_data))->format('U'))
//if( date_diff(date_create($logoutarry[$logout_i]->date_data), date_create($loginarry[$login_i+1]->date_data)) > 0 )
{
echo ("<tr><td>".$loginarry[$login_i]->date_data."</td><td>Data Not Available</td><td>Data Not Available</td></tr>");
$login_i++;
continue;
}
//
if( (new DateTime($loginarry[$login_i]->date_data))->format('U') > (new DateTime($logoutarry[$logout_i]->date_data))->format('U') )
//if( date_diff(date_create($loginarry[$login_i]->date_data), date_create($logoutarry[$logout_i]->date_data)) > 0 )
{
echo ("<tr><td>Data Not Available </td><td>".$logoutarry[$logout_i]->date_data."\n</td><td>Data Not Available</td></tr>");
$logout_i++;
continue;
}
//if more logout entries then skip to last logout entry
if( $login_i < (count($loginarry)-1) && $logout_i < (count($logoutarry)-1) && (new DateTime($logoutarry[$logout_i+1]->date_data))->format('U') < (new DateTime($loginarry[$login_i+1]->date_data))->format('U'))
{
echo(" <tr><td> Data Not Available</td> <td>".$logoutarry[$logout_i]->date_data."\n</td> <td>Data Not Available</td> </tr>");
$logout_i++;
continue;
}
echo ("<tr><td> ".$loginarry[$login_i]->date_data."</td> <td>".$logoutarry[$logout_i]->date_data."</td>");
$singleduration = ((new DateTime($logoutarry[$logout_i]->date_data))->format('U') - (new DateTime($loginarry[$login_i]->date_data))->format('U'));
if($singleduration > 43200)//12 hours X 3600 seconds)
{
//$timeduration += $singleduration;
echo ("<td class='danger'>Huge duration - not considered - please contact HR (".$singleduration.")</td></tr>");
}
else
{
$timeduration += $singleduration;
echo ("<td>".gmdate("H:i:s",$singleduration)."</td></tr>");
}
$login_i++;
$logout_i++;
}
echo ("</table>");
//$finaltime=gmdate("H:i:s",$timeduration);
//$timedurationstr = ConvertSecondsToStr($timeduration);
$hours = 0;
if($timeduration > 3600)
{
$hours = floor($timeduration/3600);
$timeduration -= (3600*$hours);
}
$min = 0;
if($timeduration > 60)
{
$min = floor($timeduration/60);
$timeduration -= (60*$min);
}
$seconds = $timeduration;
//echo("<div class= 'row'> <div class ='col-sm-4' style= 'float:right'><h3 class='alert alert-success'><center>Total Hours Worked ". $finaltime." Hours</center></h3></div></div>");
echo("<div class= 'row'> <div class ='col-sm-4' style= 'float:right'><h3 class='alert alert-success'><center>Total time worked: ". $hours." Hour(s) ".$min." Minute(s) ".$seconds." Second(s)</center></h3></div></div>");
//echo("<div class= 'row'> <div class ='col-sm-4' style= 'float:right'><h3 class='alert alert-success'><center>Total time worked: ". $timedurationstr." </center></h3></div></div>");
?>
模型代码:
public function all_login($emp_id)
{
$startDate = new DateTime("first day of last month");
$endDate = new DateTime("last day of last month");
$this->db->select('*');
$this->db->from('daily_data2');
$this->db->where('users.emp_id',$emp_id);
$this->db->where('entry >','100');
$this->db->where("date(date_data) BETWEEN '" . $startDate->format( 'Y-m-d' ) . "' AND '" . $endDate->format( 'Y-m-d') . "' ");
$this->db->order_by("date_data","ASC");
$this->db->join('users', 'users.emp_id = daily_data2.emp_id','inner');
$query = $this->db->get();
$res = $query->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->db->select('*');
$this->db->from('daily_data2');
$this->db->where('users.emp_id',$emp_id);
$this->db->where('entry <','100');
$this->db->where("date(date_data) BETWEEN '" . $startDate->format( 'Y-m-d' ) . "' AND '" . $endDate->format( 'Y-m-d') . "' ");
$this->db->order_by("date_data","ASC");
$this->db->join('users', 'users.emp_id = daily_data2.emp_id','inner');
$query = $this->db->get();
$res = $query->result();
return $res;
}
数组:
array (size=21)
0 =>
object(stdClass)[27]
public 'id' => string '1' (length=1)
public 'emp_id' => string '1' (length=1)
public 'name' => string 'Hiren' (length=5)
public 'last_name' => string 'Gada' (length=4)
public 'user_type' => string 'employee' (length=8)
public 'email' => string 'hiren@bizrtc.com' (length=16)
public 'password' => string '55f32a0322e3b2f1bf8f41e20bd1dd075f50cfc2becc4fd9bc85ac10eea4bf0c5be3b00596f1d8b4fda6e5ab1c3c388bc28fdc0454bc260382be86613057f045' (length=128)
public 'phone' => string '9999999999' (length=10)
public 'gender' => string 'Male' (length=4)
public 'designation' => string 'CEO' (length=3)
public 'blood_group' => string '+ve' (length=3)
public 'date_birth' => string 'DD-MM-YYYY' (length=10)
public 'status' => string 'active' (length=6)
public 'address' => string 'DD-MM-YYYY' (length=10)
1 =>
object(stdClass)[310]
public 'id' => string '2' (length=1)
public 'emp_id' => string '3' (length=1)
public 'date_data' => string '2015-12-01 18:20:15' (length=19)
public 'abc' => string '1' (length=1)
public 'def' => string '0' (length=1)
public 'entry' => string '101' (length=3)
public 'ghi' => string '0' (length=1)
public 'status' => string 'active' (length=6)
public 'name' => string 'Deepa' (length=5)
public 'last_name' => string 'Gada' (length=4)
public 'user_type' => string 'employee' (length=8)
public 'email' => string 'deepa@bizrtc.com' (length=16)
public 'password' => string '108fae131090e2cd93d569050a105f5187a3a9c2e9fef0010f4acea30d999834d4c36cd0435550a42b8c98f9a7dcbfd05cb9b1428aa596a71b44e20879e233bb' (length=128)
public 'phone' => string '9999999999' (length=10)
public 'gender' => string 'Female' (length=6)
public 'designation' => string 'Sr. Software' (length=12)
public 'blood_group' => string '+ve' (length=3)
public 'date_birth' => string 'DD-MM-YYYY' (length=10)
public 'address' => string 'DD-MM-YYYY' (length=10)
object(stdClass)[1142]
public 'id' => string '20' (length=2)
public 'emp_id' => string '22' (length=2)
public 'date_data' => string '2015-12-11 07:34:22' (length=19)
public 'abc' => string '1' (length=1)
public 'def' => string '0' (length=1)
public 'entry' => string '101' (length=3)
public 'ghi' => string '0' (length=1)
public 'status' => string 'active' (length=6)
public 'name' => string 'Parveen' (length=7)
public 'last_name' => string 'Thakur' (length=6)
public 'user_type' => string 'employee' (length=8)
public 'email' => string 'praveen@bizrtc.com' (length=18)
public 'password' => string '3eee66dbace42d2e671c52013e41de441b176dbaa0f7df33a5811b86c78b60ecb5328184bf1f5057f94817801140d7287f31c1fb06fa65550c356a33a8eec0db' (length=128)
public 'phone' => string '9999999999' (length=10)
public 'gender' => string 'Male' (length=4)
public 'designation' => string 'Support Staff' (length=13)
public 'blood_group' => string '+ve' (length=3)
public 'date_birth' => string 'DD-MM-YYYY' (length=10)
public 'address' => string 'DD-MM-YYYY' (length=10)
答案 0 :(得分:2)
试试这个
foreach ($users as $user)
{
$emp_id = $user['emp_id'];
$name = $user['name'];
$attendances_logins = $this->attendance_m->all_login($emp_id);
$attendances_logouts = $this->attendance_m->all_logout($emp_id);
foreach ($attendances_logins as $key => $value) {
$loginTime = $value['login_time'];
$logoutTime = $attendances_logouts[$key]['logout_time']''
}
$login_logout[] = array(
'emp_id' => $emp_id,
'name' =>$name,
'hours' =>$hours,
'min' => $min,
'seconds' =>$seconds,
'login' => $attendances_logins, # Count number only
'logout' => $attendances_logouts # Count number only
);
}
$this->data['logins'] = $login_logout
// load view