We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

get difference in  time from 2 timestamps

phper
phper asked
on
Medium Priority
416 Views
Last Modified: 2013-12-12
I have 2 timestamp columns in a db table. One is TimeIn the other is TimeOut.

What I'm trying to do is find the total time, i.e. the difference between the 2 columns. What I thought would work is just subtract the 2 but I get a weird responses.

Here's my query:
SELECT
TimeOut,
TimeIn,
(TimeOut-TimeIn) as TT
FROM
project_time

But what I get is the following:
2/12/2009 10:37:40 | 2/12/2009 10:37:30 | 10
2/12/2009 10:39:21 | 2/12/2009 10:37:21 | 200
2/12/2009 10:40:00 | 2/12/2009 10:39:30 | 70
2/12/2009 10:53:00 | 2/12/2009 10:52:00 | 100
2/12/2009 11:06:28 | 2/12/2009 10:57:36 | 4892
2/12/2009 11:08:04 | 2/12/2009 11:06:49 | 155

What I want is the total Seconds. From there I can compute the Min & Hours. I've also tried both Timestamp and Datetime but both had the same results.
Comment
Watch Question

Top Expert 2008
Commented:
Which version of mysql?

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff


SELECT
TimeOut,
TimeIn,
TIMEDIFF(TimeIn,TimeOut) as TT
FROM
project_time

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2008

Commented:
You could also transform the datetime columns to a unix timestamp, and subtract:
SELECT
TimeOut,
TimeIn,
unix_timestamp(TimeOut)-
unix_timestamp(TimeIn) as TT
FROM
project_time

Open in new window

Author

Commented:
It's
TIMEDIFF(TimeOut,TimeIn) as TT

But you got it, thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.