Time diff calculations where date and time are in seperate columns
Posted
by pedalpete
on Stack Overflow
See other posts from Stack Overflow
or by pedalpete
Published on 2010-05-03T16:41:05Z
Indexed on
2010/05/03
16:48 UTC
Read the original article
Hit count: 210
mysql-query
|datetime
I've got a query where I'm trying to get the hours in duration (eg 6.5 hours) between two different times.
In my database, time and date are held in different fields so I can efficiently query on just a startDate, or endDate as I never query specifically on time.
My query looks like this
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(endTime,startTime)),0) FROM events WHERE user=18
Sometimes an event will go overnight, so the difference between times needs to take into account the differences between the dates as well.
I've been trying
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(CONCAT(endDate,' ',endTime),CONCAT(startDate,' ',startTime))),0) FROM events WHERE user=18
Unfortunately I only get errors when I do this, and I can't seem to combine the two fields into a single timestamp.
© Stack Overflow or respective owner