w3resource

MySQL TIMEDIFF() function

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.

This function is useful in -

  • TIMEDIFF() is essential for time-based analysis by enabling you to measure the duration of events or actions.
  • It supports time series analysis by allowing you to calculate the time intervals between data points.
  • TIMEDIFF() is useful for filtering records or data based on time criteria, such as selecting records within a specific time range.
  • TIMEDIFF() is valuable in real-time applications where precise time measurements are crucial for decision-making.
  • It enables you to present the calculated time difference in a custom format, enhancing data presentation.
  • It can be used to measure the duration of events or activities, providing valuable insights for planning and resource allocation.
  • The function facilitates comparing time values, allowing you to determine which event or action occurred first or lasted longer.

Syntax:

TIMEDIFF(expr1,expr2)

Arguments:

Name Description
expr1 A datetime value.
expr2 A datetime value.

Syntax Diagram:

MySQL TIMEDIFF() Function - Syntax Diagram

MySQL Version: 8.0


Pictorial Presentation:

Pictorial Presentation of MySQL TIMEDIFF() function

Example:

The following statement will return the difference between two datetime values 2009-05-18 15:45:57.005678 and 2009-05-18 13:40:50.005670.

Code:

SELECT TIMEDIFF('2009-05-18 15:45:57.005678','2009-05-18 13:40:50.005670');

Output:

mysql> SELECT TIMEDIFF('2009-05-18 15:45:57.005678','2009-05-18 13:40:50.005670');
+---------------------------------------------------------------------+
| TIMEDIFF('2009-05-18 15:45:57.005678','2009-05-18 13:40:50.005670') |
+---------------------------------------------------------------------+
| 02:05:07.000008                                                     | 
+---------------------------------------------------------------------+
1 row in set (0.00 sec)

Video Presentation:

All Date and Time Functions:

Click here to see the MySQL Date and time functions.

Previous: TIME()
Next: TIMESTAMP()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql/date-and-time-functions/mysql-timediff-function.php