php - 在 PHP MySQL 中计算考勤

标签 php mysql

我正在与 Laravel 合作,我有一个包含所有用户出席情况的表。 每行都有一个标志,说明用户是否登录或注销。我想计算用户的总工作时间。

这是我的表格和示例数据

id | user_id |      date     |   timestamp   | status |
 1 |    1    |   2018-05-10  |    17:15:31   |   out  |
 1 |    1    |   2018-05-10  |    13:15:31   |   in   |
 1 |    1    |   2018-05-10  |    12:15:31   |   out  |
 1 |    1    |   2018-05-10  |    08:01:31   |   in   |

我想计算总工作时间

$logs = DB::table('attendances as at')
        ->join('infos as in','in.user_id','=','at.user_id')
        ->select('in.name','in.avatar','at.*')
        ->orderBy('id','desc')
        ->where('at.user_id',$id)
        ->get();

 $total_hours = [];

    for($i=0; $i < count($logs)-1; $i++ ){

         if($logs[$i]->status == 'out'){
           $dattime1 = new DateTime($logs[$i]->dt.' '. $logs[$i]->time);
           $dattime2 = new DateTime($logs[$i+1]->dt.' '. $logs[$i+1]->time);

            $total_hours[] = $dattime1 ->diff($dattime2);
         }

    }

 $working_hours = array_sum($total_hours);

这是获得准确结果的最佳方法吗?请帮忙。 谢谢

最佳答案

你可以这样尝试吗?

$time1 = "17:15:00";
$time2 = "00:30:00";

$strtotime1 = strtotime($time1);
$strtotime2 = strtotime($time2);
 $o = ($strtotime1) + ($strtotime2);
echo $time = date("h:i:s A T",$o);

输出将是这样的:

05:45:00 PM UTC 

关于php - 在 PHP MySQL 中计算考勤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50267974/

相关文章:

php - Laravel 5 显示图像

php - 通过 SPF 验证电子邮件

mysql - 获取按付款类型分组的付款总额

MySQL:从 6 个巨大的相同表中优化 SELECT,这些表具有按时间戳拆分的不同数据

mysql - SQL Server Management Studio 查看和更改表

php - Drupal 电子邮件变量未出现

向雷鸟发送 html 邮件时,PHP 邮件无法显示链接

php - 直接从Mysql查询数据生成报告(使用groupby、count)

mysql - 对 MySQL 语句中的分组行进行排序

php - 在 while 循环中,结果错误