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 > Datediff in Hours?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Jan 2009
Posts: 2
Red face Datediff in Hours?

How can I subtract two datestamps (yyyy-mm-dd hh:mm:ss) to give the result in hours?
Reply With Quote
  #2 (permalink)  
Old
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,527
Quote:
Originally Posted by Merlinius
How can I subtract two datestamps (yyyy-mm-dd hh:mm:ss) to give the result in hours?
You can convert both times to seconds using unix_timestamp. Then subtract one from other to get number of seconds between both times. Then divide by 3600 to get the number of hours ie
Code:
select ( unix_timestamp( "2008-01-28 10:00" ) - 
         unix_timestamp("2008-01-27 09:00" ) ) / 3600
Note that this only works if both dates are after 1970.
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Jan 2009
Posts: 2
Quote:
Originally Posted by mike_bike_kite
You can convert both times to seconds using unix_timestamp. Then subtract one from other to get number of seconds between both times. Then divide by 3600 to get the number of hours ie
Code:
select ( unix_timestamp( "2008-01-28 10:00" ) - 
         unix_timestamp("2008-01-27 09:00" ) ) / 3600
Note that this only works if both dates are after 1970.

Nice one Mike
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