Mysql Timediff Function

MySQL: TIMEDIFF Function

This tutorial explains how to use the MySQL TIMEDIFF function with syntax and examples.

Description

The MySQL TIMEDIFF function returns the difference (expressed as a time value) between two time/datetime values.

Syntax

The syntax for the TIMEDIFF function in MySQL is:

TIMEDIFF( time1, time2 )

Parameters or Arguments

time1 and time2

The two time/datetime to calculate the difference between. The calculation is time1 - time2.

Note

  • A time value can range from -838:59:59' to '838:59:59'.
  • When using the TIMEDIFF function, both time1 and time2 must be both time values, or both datetime values. You can not have one be a time value and the other be a datetime value, or the TIMEDIFF function will return a NULL value.

Applies To

The TIMEDIFF function can be used in the following versions of MySQL:

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1.1

Example

Let's look at some MySQL TIMEDIFF function examples and explore how to use the TIMEDIFF function in MySQL.

For example:

mysql> SELECT TIMEDIFF('2014-02-17 12:10:08', '2014-02-17 12:10:07');
Output: '00:00:01'

mysql> SELECT TIMEDIFF('12:10:08', '12:10:07');
Output: '00:00:01'

mysql> SELECT TIMEDIFF('11:41:14', '12:10:08');
Output: '-00:28:54'

mysql> SELECT TIMEDIFF('2014-02-17 12:10:08', '2014-02-16 12:10:08');
Output: '24:00:00'

mysql> SELECT TIMEDIFF('2014-02-17 12:10:08', '12:10:08');
Output: NULL