Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in minutes from two time fields in MySQL

Tags:

time

mysql

I set up my MySQL database with the field 'time'

It is not HH:MM in the traditional sense, it is the time an event occurred, so an event with the value of 5:45 occurred with 5 minutes 45 seconds left in a game. 12:25 occurred with 12 minutes and 25 seconds left, etc.

I would like to be able to find out the total time elapsed, so if I have an event that occurred at 12:25 and the next event occurred at 5:45 I want to be able to get the difference, which would be equal to 6:40. Then, I would like to express this as a decimal, in this case 6.67.

Is this possible?

Thanks in advance.

like image 897
NeilG Avatar asked Feb 21 '11 19:02

NeilG


People also ask

How do I compare time differences in MySQL?

MySQL TIMEDIFF() Function The TIMEDIFF() function returns the difference between two time/datetime expressions. Note: time1 and time2 should be in the same format, and the calculation is time1 - time2.

How do I get the time difference between two columns in SQL?

To calculate the difference between the arrival and the departure in T-SQL, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument can be microsecond , second , minute , hour , day , week , month , quarter , or year .

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

Discussion: If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.

How do I calculate hours between two dates in MySQL?

TIMEDIFF() function MySQL TIMEDIFF() returns the differences between two time or datetime expressions. It is to be noted that two expressions must be the same type. A datetime value.


1 Answers

For me this worked:

TIMESTAMPDIFF(MINUTE, T0.created, T0.modified) 

Reference: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampdiff

like image 82
A T Avatar answered Sep 23 '22 23:09

A T