How to get the difference between two dates rounded to hours

前端 未结 8 1301

I\'ll illustrate what I would like to get in the following example:

\'2010-09-01 03:00:00\' - \'2010-09-01 00:10:00\'

Using TIMEDIFF(

相关标签:
8条回答
  • 2020-12-04 21:39

    I think a more complete answer is

    select time_format(timediff(onedateinstring,anotherdateinstring),'%H:%i:%s')
    

    This allows you to see the hours, minutes, seconds, etc. that you wish to see in the format you want...

    More information on time_format: http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_time-format

    0 讨论(0)
  • 2020-12-04 21:40
    select time_to_sec(timediff('2010-09-01 03:00:00', '2010-09-01 00:10:00' )) / 3600;
    
    +-----------------------------------------------------------------------------+
    | time_to_sec(timediff('2010-09-01 03:00:00', '2010-09-01 00:10:00' )) / 3600 |
    +-----------------------------------------------------------------------------+
    |                                                                      2.8333 | 
    +-----------------------------------------------------------------------------+
    
    0 讨论(0)
  • 2020-12-04 21:48

    Use TIMESTAMPDIFF for exact number of hours :

    SELECT 
          b.`Start_Date`, 
          b.`End_Date`, 
          TIMESTAMPDIFF(HOUR, b.`Start_Date`, b.`End_Date`) AS `HOURS` 
    FROM my_table b;
    
    0 讨论(0)
  • 2020-12-04 21:48

    MySQL get the number of hours between timestamps:

    select timestampdiff(second,'2010-09-01 00:10:00', '2010-09-01 03:00:00')/3600;
    
    0 讨论(0)
  • 2020-12-04 21:52

    Using UNIX_TIMESTAMP:

    SELECT (UNIX_TIMESTAMP(End)-UNIX_TIMESTAMP(Start))/3600
    AS Difference_in_hours 
    FROM Table
    
    0 讨论(0)
  • 2020-12-04 21:54

    You could try the following:

    time_format(timediff(max(l.fecha), min(l.fecha) ),'%H:%m') //Hora y minutos
    
    0 讨论(0)
提交回复
热议问题