In MySQL, you may have encountered a scenario where you need to determine the time elapsed between two 'time' fields. These fields typically represent the time remaining in an event rather than adhering to the traditional HH:MM format.
To accurately calculate the time difference, you can utilize the TIMESTAMPDIFF() function. Here's how it works:
<code class="sql">TIMESTAMPDIFF(MINUTE, Time_Field_1, Time_Field_2)</code>
In this function:
The function returns the time difference in minutes. For instance, if Time_Field_1 is 12:25 and Time_Field_2 is 5:45, the result would be 405 minutes.
To express this difference as a decimal, simply divide the result by 60:
<code class="sql">TIMESTAMPDIFF(MINUTE, Time_Field_1, Time_Field_2)/60</code>
In the aforementioned example, this would yield a decimal value of 6.75.
By utilizing this approach, you can effectively calculate the time elapsed between events and obtain the result in the desired format.
The above is the detailed content of How to Calculate Time Elapsed Between MySQL \'Time\' Fields?. For more information, please follow other related articles on the PHP Chinese website!