mysql time 838,超过MySQL的TIME值限制838:59:59

The title might be a bit confusing so allow me to explain. I'm using a table to record my work logs. Every day I'll create an entry stating from what time to what time I have worked and I'll add a comment describing what I did.

I then use a query to compare the timestamps to figure out exactly how many hours and minutes I have worked that day. Additionally, I use a query to calculate the sum of hours and minutes I have worked the entire year. That's where I'm running into a problem. My query is as follows.

SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(entry_end_time, entry_start_time)))), '%H:%i')

AS total FROM entry

WHERE entry_date BETWEEN '2012-01-01' AND '2012-12-31' AND user_id = 3

By default, MySQL TIME fields allow a time range of '-838:59:59' to '838:59:59'. I have currently logged more than 900 hours of work this year though, and I want the result of my query to reflect this. Instead, the result is 838:59:59, which makes sense because that is the limit.

Is there any way around this so the result of the query can go beyond 839 hours, or would I have to use something like PHP to go over the entire table and add it all up? I kind of want to avoid that if possible.

解决方案

I'd just retrieve the total number of seconds worked, and convert to hours/minutes as required in the presentation layer of my application (it is, after all, a simple case of division by 60):

$dbh = new PDO("mysql:dbname=$dbname", $username, $password);

$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);

$qry = $dbh->prepare('

SELECT SUM(TIME_TO_SEC(entry_end_time)-TIME_TO_SEC(entry_start_time))

FROM entry

WHERE entry_date BETWEEN :start_date AND :end_date

AND user_id = :user_id

');

$qry->execute([

':start_date' => '2012-01-01',

':end_date' => '2012-12-31',

':user_id' => 3

]);

list ($totalMins, $remngSecs) = gmp_div_qr($qry->fetchColumn(), 60);

list ($totalHour, $remngMins) = gmp_div_qr($totalMins, 60);

echo "Worked a total of $totalHour:$remngMins:$remngSecs.";

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值