doudouwd2017 2015-01-07 00:52
浏览 20
已采纳

使用MySQL查询在PHP中进行时间计算

I have a report to build in PHP where I need time difference times hourly rate for an employee pay.

For example:

Employee     Clock-In           Clock-Out     Total-Hours  Rate  Salary
Mike    2015-01-01 22:57:09 2015-01-02 11:48:14 12:51:05    10  128.51
Mike    2015-01-03 11:48:51 2015-01-06 22:19:27 82:30:36    10  825.10
Mike    2015-01-06 21:19:40 2015-01-06 22:00:00 00:40:20    10  6.72

My query is below

SELECT tt.id,ts.fname,tt.punch_in,tt.punch_out,
SEC_TO_TIME(TIMESTAMPDIFF(SECOND,tt.punch_in,tt.punch_out)) as working_hours_new,ter.hourlyrate,
((SUBSTRING(SEC_TO_TIME(TIMESTAMPDIFF(SECOND,tt.punch_in,tt.punch_out)),1,2) + SUBSTRING(SEC_TO_TIME(TIMESTAMPDIFF(SECOND,tt.punch_in,tt.punch_out)),4,2)/60 + SUBSTRING(SEC_TO_TIME(TIMESTAMPDIFF(SECOND,tt.punch_in,tt.punch_out)),7,2)/3600) * ter.hourlyrate) as pay
from tbl_times as tt left join tbl_server as ts on ts.serverid = tt.user_id left join tbl_employee_rates as ter on ter.userid=tt.user_id 
where ts.locationid = ? and ts.active = 1 and ter.stauts = 1 and date(punch_in) >=  ? and date(punch_out) <= ?

If this is not an easy way to do, should I do it in PHP... any suggestions?

  • 写回答

2条回答 默认 最新

  • douzhao4071 2015-01-07 03:31
    关注

    Why not try: (assuming mysql)

    ((UNIX_TIMESTAMP(tt.punch_out) - UNIX_TIMESTAMP(tt.punch_in)) / 3600) * ter.hourlyrate AS salary
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟