Click to See Complete Forum and Search --> : [RESOLVED] MySQL Time Difference BUG


cancer10
02-14-2009, 12:55 AM
Hi

Not sure if this is a bug or not.


I am doing a sql query for time difference using the TIMEDIFF function.

It works for all other but not when not when you are doing a time difference for start time 23:00:00 (11 PM night) and end time 00:30:00 (12 AM Midnight)

if you take a look, the difference is 1.5 hours, but the sql tells me that the difference is 22 hours, 30 minutes



Any solution to this?

Thanx

NogDog
02-14-2009, 01:14 AM
If you are only entering times, then I believe MySQL will assume they are both for the current date, thus it is getting the difference between 23:00:00 tonight and 00:30:00 this morning, which is, in fact 22-1/2 hours. Or in other words: you are not entering a "start time" and "end time", just two arbitrary times, the second being subtracted from the first.

What you need is to have a datetime (or timestamp) value for each value.

cancer10
02-14-2009, 01:16 AM
An example would help me very much.


thanx

NogDog
02-14-2009, 02:48 AM
SELECT TIMEDIFF( '22:00:00', '01:00:00' ) ,
TIMEDIFF( '2009-02-13 22:00:00', '2009-02-14 01:00:00' ) ,
TIMEDIFF( '2009-02-14 01:00:00', '2009-02-13 22:00:00' )

Result:

21:00:00 -03:00:00 03:00:00

So most likely you probably want the 3rd version, where you have the later date-time as the first argument to TIMEDIFF().

cancer10
02-14-2009, 02:49 AM
Problem solved.

Thanx.

Mods: Please close this topic.

NogDog
02-14-2009, 02:52 AM
Mods: Please close this topic.

As the topic starter, you can click on the "Thread Tools" option in the thread menu bar and select "Mark as Resolved" (or however it's worded).