Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating time difference between 2 dates in minutes

I have a field of time Timestamp in my MySQL database which is mapped to a DATE datatype in my bean. Now I want a query by which I can fetch all records in the database for which the difference between the current timestamp and the one stored in the database is > 20 minutes.

How can I do it?

What i want is:

SELECT * FROM MyTab T WHERE T.runTime - now > 20 minutes 

Are there any MySQL functions for this, or any way to do this in SQL?

like image 431
Akshay Avatar asked Oct 03 '11 14:10

Akshay


People also ask

How do you find the difference between two dates in minutes?

We subtract time/dates in excel to get the number of days. Since a day has 1440 (24*60) minutes, we multiply the result by 1440 to get the exact number of minutes.

How do you calculate time difference quickly?

Compute time difference manuallySubtract all end times from hours to minutes. If the start minutes is higher than the end minutes, subtract an hour from the end time hours and add 60 minutes to the end minutes. Proceed to subtract the start time minutes to the end minutes. Subtract the hours too from end to start.


1 Answers

I think you could use TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2) something like

select * from MyTab T where TIMESTAMPDIFF(MINUTE,T.runTime,NOW()) > 20 
like image 73
Nicola Peluchetti Avatar answered Oct 04 '22 23:10

Nicola Peluchetti