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

Asked

Viewed 471 times

1

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

example:

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

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

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

2 answers

7

Use the function timediff() he looks like datediff() but returns the time(hh:mm:ss) of two dates. To compare only the time, use function HOUR() thus 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
  • Only the information needed to use the HOUR(TIMEDIFF()) together :D

  • Already edited @Wallacemaxters! 1111

  • Sorry to cancel, but @Bacco pointed out a problem in doing it the last way :|

7


Browser other questions tagged

You are not signed in. Login or sign up in order to post.