1

I have this query:

SELECT sum(TIMEDIFF(`time_out`, `time_in`)) as `total` FROM `user_log` WHERE `uid` = '1' AND `time_out` <> '0000-00-00 00:00:00'

Then I calculate the seconds to hours:

$total_difference = (($difference->total) / 60 / 60);

But this is showing me 22,352125 hours, which is impossible...

I've been testing and I have 2 records showing up if I don't use SUM:

SELECT TIMEDIFF(`time_out`, `time_in`) as `total` FROM `user_log` WHERE `uid` = '1' AND `time_out` <> '0000-00-00 00:00:00'
  1. 08:00:02
  2. 00:00:11

This result is far below the 22 hours I get when I use SUM in my query. Any help? I don't know what I'm doing wrong.

Thanks!

6
  • $total_difference = (($difference->total) / 3600); try this Commented May 8, 2015 at 14:05
  • 3
    try SUM(TIME_TO_SEC(TIMEDIFF(time_out, time_in))) Commented May 8, 2015 at 14:06
  • @anant kumar singh: It's still the same result. :\ Commented May 8, 2015 at 14:06
  • @amdixon: It looks like that did the job. Thanks! Commented May 8, 2015 at 14:08
  • @amdixon, I believe you should write it as an answer since it solved the problem Commented May 8, 2015 at 14:10

1 Answer 1

3

Can adjust the query to use TIME_TO_SEC - this will convert the return value of TIMEDIFF to a numeric number of seconds, which can sensibly be summed

adjusted query

SELECT SUM(TIME_TO_SEC(TIMEDIFF(time_out, time_in))) as total 
FROM user_log 
WHERE uid = '1' AND time_out <> '0000-00-00 00:00:00'
;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.