Home >Database >Mysql Tutorial >How to Calculate Time Difference in Minutes from MySQL Time Fields?

How to Calculate Time Difference in Minutes from MySQL Time Fields?

Barbara Streisand
Barbara StreisandOriginal
2024-10-29 13:47:29613browse

How to Calculate Time Difference in Minutes from MySQL Time Fields?

Determining Time Difference in Minutes from MySQL Time Fields

You have encountered the need to calculate the time difference between two events recorded as 'time' fields in your MySQL database. These fields do not follow the conventional HH:MM format but represent the time remaining in an event. For instance, a value of 5:45 signifies an occurrence with 5 minutes and 45 seconds remaining.

Solution:

To achieve your desired result, you can leverage the TIMESTAMPDIFF() function, which calculates the difference between two timestamps. Employing this function with your specific case, you can determine the time elapsed between the two events as follows:

<code class="sql">TIMESTAMPDIFF(MINUTE, T0.created, T0.modified)</code>

In this expression:

  • T0 represents the table containing the time fields.
  • created is the name of the field indicating the time of the first event.
  • modified is the name of the field indicating the time of the second event.

The result will be the time difference expressed in minutes. To obtain the decimal representation, you can simply divide the difference by 60.

The above is the detailed content of How to Calculate Time Difference in Minutes from MySQL Time Fields?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn