我有代码来计算员工的总工作时间。 我提供了登录条目数组和注销数组。因此,该代码计算员工工作的总小时数,秒数和分钟数。
这是代码:
<?php
$loginarry = $attendances;
$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>");
?>
所以最后它返回了员工工作的小时数。这段代码在我看来。
现在我想为数据库中的每个用户计算这些值。 我不知道如何循环所有用户并运行此代码,以便我可以获得每个用户的总工作时间。
在控制器中,我会像这样获取用户和登录和注销条目。
$users = $this->data['users'] = $this->user_m->get();
$loginarry = $this->data['attendances'] = $this->attendance_m->all_login();
$logoutarry =$this->data['attendances_logouts'] =$this->attendance_m->all_logout();
$this->data['subview'] = 'admin/dashboard/index';
$this->load->view('admin/_layout_main',$this->data);
现在如何显示每个用户的总工作时间?
这是我获取登录和退出条目的代码
ublic function all_login($emp_id = NULL)
{
$startDate = new DateTime("first day of last month");
$endDate = new DateTime("last day of last month");
$emp_id = $this->session->userdata('emp_id');
$this->db->select('*');
$this->db->from('daily_data2');
$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");
$query = $this->db->get();
$res = $query->result();
return $res;
}
public function all_logout()
{
$startDate = new DateTime("first day of last month");
$endDate = new DateTime("last day of last month");
$emp_id = $this->session->userdata('emp_id');
$this->db->select('*');
$this->db->from('daily_data2');
$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");
$query = $this->db->get();
$res = $query->result();
return $res;
}
答案 0 :(得分:1)
如果这是你一直计算的方式,也许你应该以出勤作为参数来制作一个功能。然后你可以获得用户列表作为出勤并循环它
public function calculatedTime(attendance){
//Your coding here
}
并且你可以循环它以获得时间
foreach($loginarry as $att){
$this->calculatedTime($att);
}
尝试使编码尽可能简单,如果需要重复编码,只需创建一个函数并调用它。