How to get the difference between dates and turn into hours?

1

In MYSQL, I know I can return a difference between dates using the DATE_DIFF function.

example:

 SELECT * FROM table WHERE DATEDIFF(NOW(), created) <= ?

However, I need to return the difference in day converted to hours in this DATEDIFF . Well, I need to compare this difference to see if a certain deadline has already expired (and this deadline is hours, not days).

How can I get the value in hours of a DATEDIFF in MYSQL?

    
asked by anonymous 16.06.2016 / 15:29

2 answers

7

A good option in this case is to use TIMESTAMPDIFF , which already gives the result to the desired extent:

  SELECT TIMESTAMPDIFF( HOUR, created, NOW() );

You can use SECONDS , or other measures instead of HOUR , depending on the need.

  

link

    
16.06.2016 / 16:23
7

Use the timediff () function is similar to datediff() but returns the time (hh: mm: ss) of two dates. To compare just the time, use HOUR() function in this way only the time is returned, as in the third example.

Example:

SELECT * FROM table WHERE TIMEDIFF(NOW(), created) <= ?

SELECT TIMEDIFF('2016-06-16 00:00:00', '2016-06-15 13:20:51') //10:39:09

SELECT HOUR(TIMEDIFF('2016-06-16 00:00:00', '2016-06-15 13:20:51')) //10
    
16.06.2016 / 15:37