Results 1 to 3 of 3
  1. #1
    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?

  2. #2
    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.

  3. #3
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •