dongliantong3229 2018-05-09 13:30
浏览 100
已采纳

从日期时间计算加班时间

I am trying to calculate overtime based on a overtimefactor of 07:24:00:0000 hours

My database looks like this

| id  |user_id|     starttime       | endtime
|  0  |   1   | 2018-05-09 04:30:00 | 2018-05-09 017:30:00
|  1  |   1   | 2018-05-10 06:30:00 | 2018-05-10 017:30:00
|  2  |   1   | 2018-05-11 04:30:00 | 2018-05-11 015:30:00

I am having a hard time figuring out the selectstatement that would do the job - I want ALL combined overtime as a result

My function i have now looks like this:

public function getOvertimeTotal($id, $selectedMonth) {
      $this->db->query("SELECT 
      COUNT(SUBTIME(TIMESTAMPDIFF(HOUR, starttime, endtime), '07:24:00:0000')) as overTimeTotal
      FROM hours
      WHERE user_id = :id
      AND month(starttid) = :selectedMonth");

      $this->db->bind(':id', $id);
      $this->db->bind(':selectedMonth', $selectedMonth);

      $row = $this->db->single();

      return $row;

    }

What am i doing wrong?

  • 写回答

2条回答 默认 最新

  • doutuoji8418 2018-05-09 13:57
    关注

    This should help you calculate overtime in fractional hours,

    SUM((TIMESTAMPDIFF(SECOND, starttime, endtime) - TIME_TO_SEC('07:24:00:0000')) / 3600)
    

    SQLFiddle link


    If you need the value converted back in time remove division by 3600 part from above and use SEC_TO_TIME() function

    SEC_TO_TIME(SUM(TIMESTAMPDIFF(SECOND, starttime, endtime) - TIME_TO_SEC('07:24:00:0000')))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊