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

on
Medium Priority
416 Views
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

## View Solution Only

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
``````

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

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
``````

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

But you got it, thanks
##### Thanks for using Experts Exchange.

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