If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > difference between two dates

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-30-04, 08:10
m.inckle m.inckle is offline
Registered User
 
Join Date: Feb 2004
Posts: 24
difference between two dates

What is the best way to find the difference between two dates in a database??

I have a 'received' and a 'replied' field and i want to find the response time

Please can anyone help
Reply With Quote
  #2 (permalink)  
Old 07-01-04, 04:04
snorp snorp is offline
Registered User
 
Join Date: Apr 2004
Location: Europe->Sweden->Stockholm
Posts: 71
I don't know if this is the best method, but I usually want the difference in seconds and then UNIX_TIMESTAMP() is the way to go.

So
SELECT id, UNIX_TIMESTAMP(replied)-UNIX_TIMESTAMP(received) AS resptime FROM tab;
would give you the difference between the two times in seconds.
Reply With Quote
  #3 (permalink)  
Old 07-01-04, 08:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you can then use the SEC_TO_TIME function to convert the seconds to HH:MM format

note: this works only if the seconds are less than 86400 (one day), but you can use CASE expressions with some further math to extract the remainder (modulus)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 07-03-04, 19:38
yellowmarker yellowmarker is offline
Registered User
 
Join Date: Jul 2004
Location: Dundee, Scotland
Posts: 107
DATEDIFF() was added in MySQL 4.1.1.

DATEDIFF(expr,expr2)
DATEDIFF() returns the number of days between the start date expr and the end date expr2. expr and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

mysql> SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');
-> 1
mysql> SELECT DATEDIFF('1997-11-30 23:59:59','1997-12-31');
-> -31
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On