简体   繁体   English

如何参考签入和第二天结帐时间在MYSQL中获得带日期和时间的总工作时间

[英]how to get total no of working hours with date & time in MYSQL with reference to checkin & next day checkout time

Below table shows the checkin & checkout time of employee 下表显示了员工的签到和结帐时间

employee_oid report_date report_time employee_oid report_date report_time

 11     2014-12-01 08:02:31
 21     2014-12-01 08:13:04
 06     2014-12-01 08:13:04
 11     2014-12-02 18:03:41
 21     2014-12-02 16:36:02
 06     2014-12-03 16:36:02
 11     2014-12-04 08:02:31
 06     2014-12-04 08:36:02
 21     2014-12-02 08:36:02
 11     2014-12-04 16:34:20
 06     2014-12-05 08:36:02
 21     2014-12-05 08:50:50

What's the algorithm of working hours? 工作时间的算法是什么? Supposing working hours equals checkout time minus checkin time, I've got this SQL: 假设工作时间等于签出时间减去签入时间,我得到以下SQL:

SELECT 
  Emp_id,
  report_date,
  MIN(report_time) AS checkin_time,
  MAX(report_time) AS checkout_time,
  DATE_FORMAT (MAX(report_time) - MIN(report_time), '%h') AS working_hours
FROM
  report 
GROUP BY Emp_id;

But, it sucks when employees go to work again and again.... 但是,当员工一次又一次地上班时,情况就糟透了。

I'm wandering why the employees should checkout in next day. 我在徘徊为什么员工第二天要结帐。 It's also confusing when I saw employee 06 who checked out after two days. 当我看到06号员工两天后退房时,这也令人困惑。

For night shift, I tried this SQL which might be helpful: 对于夜班,我尝试使用此SQL可能会有所帮助:

SELECT 
  a.`id` AS employ_id,
  ADDTIME(MIN(a.adjustedDateTime), '12:00:00') AS check_in_time,
  ADDTIME(MAX(a.adjustedDateTime), '12:00:00') AS check_out_time,
  TIMEDIFF(
    MAX(a.adjustedDateTime),
    MIN(a.adjustedDateTime)
  ) AS working_time,
  COUNT(0) AS report_times,
  DATE_FORMAT(a.adjustedDateTime, '%Y-%m-%d') AS report_date
FROM
  (SELECT 
    `Emp_id` AS id,
    SUBTIME(
      CONCAT(report_date, ' ', report_time),
      '12:00:00'
    ) AS adjustedDateTime 
  FROM
    report) a 
GROUP BY a.`id`,
  DATE_FORMAT(a.adjustedDateTime, '%Y-%m-%d') 
ORDER BY a.`id`, report_date;

I used SUBTIME to make the report_time which in one shift be gathered into one day. 我用SUBTIME制作了report_time,将它在一天内收集到一天。 Then group by the date. 然后按日期分组。 Thus got the result. 从而得到了结果。

<?php
require_once('dbconn.php');
$q="select employee_oid, min(concat(report_date,' ',report_time)) as checkin, max(concat(report_date,' ',report_time)) as checkout from report_master where 1 group by employee_oid,report_date;";
$rec = mysql_query($q);
$k=0;
$i=0;
$previous_empid=0;
if(mysql_num_rows($rec) > 0)
{ 
$arr=$arr2 = array();

while($row = mysql_fetch_array($rec))

{
$employee_oid = $row['employee_oid'];
$minmumdate = $row['checkin'];
$maxdate = $row['checkout'];
$timestamp1 = strtotime($minmumdate);
$timestamp2 = strtotime($maxdate);
$hour = abs($timestamp2 - $timestamp1)/(60*60);

$arr[$employee_oid][] = array(
'hours'=>$hour
);

}
        //print_r($arr);
foreach($arr as $key=>$result){
//echo $key.'   key value';
$total_hours=0;
foreach($result as $key1=>$result1){
$total_hours= $total_hours + $result1['hours'];
}
if($total_hours>176) {
    $extra_hours = $total_hours - 176;
    $extra_wages = $extra_hours * 31.25;
                }
$arr2[$key][] = array(
    'total_hours'=>$total_hours,
    'extra_hours'=>$extra_hours,
    'extra_wages' =>$extra_wages
    );

}

print("<pre>");
print_r($arr2);
exit();
foreach($arr2 as  $key=>$result){
echo $key.' = > total_hours '.$result['total_hours'].' extra_hours '.$result['extra_hours'].' extra_wages '.$result['extra_wages'].'</br></br>';
            }
    }

?>

I have this query:- 我有这个查询:

select DATE_FORMAT(yb_date, '%d') AS yb_date,'2' as roll,'asd' AS staff_name,CASE WHEN yb.yb_date> '5050-12-31' THEN '' WHEN yb.yb_date<'0' THEN '' WHEN yb.yb_date> CURDATE() THEN '' WHEN yb.day_status = 'Holiday' AND date(a.attend_date) IS NULL THEN 'H' WHEN date(a.attend_date) IS NULL THEN 'A' ELSE 'P' END AS status,CASE WHEN time(min(attend_date)) IS NULL THEN '00:00:00' ELSE time(min(attend_date)) END as in_time,CASE WHEN time(max(attend_date)) IS NULL THEN '00:00:00' ELSE time(max(attend_date)) END as out_time,CASE WHEN timediff(time(max(attend_date)),time(min(attend_date))) IS NULL THEN '00:00:00' ELSE timediff(time(max(attend_date)),time(min(attend_date))) END as duration from yearbook yb LEFT join attendance_essl a ON date(a.attend_date)=yb.yb_date AND a.emp_id='000012' where yb.yb_date between '2019-09-29' AND '2019-10-05' group by yb.yb_date,a.emp_id order by yb.yb_date ASC

Output is :- 输出是:-

yb_date
roll
staff_name
status
in_time
out_time
duration
29
2
asd
H
00:00:00
00:00:00
00:00:00
30
2
asd
A
00:00:00
00:00:00
00:00:00
01
2
asd
A
00:00:00
00:00:00
00:00:00
02
2
asd
P
17:10:03
17:10:03
00:00:00
03
2
asd
P
15:10:03
15:10:03
00:00:00
04
2
asd
P
06:10:03
18:10:03
12:00:00
05
2
asd
H
00:00:00
00:00:00
00:00:00

If I add night shift means how to get 如果我加夜班意味着怎么去

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM