MySQL - TIMEDIFF() Function

The DATE, DATETIME and TIMESTAMP datatypes in MySQL are used to store the date, date and time, time stamp values respectively. Where a time stamp is a numerical value representing the number of milliseconds from '1970-01-01 00:00:01' UTC (epoch) to the specified time. MySQL provides a set of functions to manipulate these values.
The MYSQL TIMEDIFF() function accepts two time or, date-time values as parameters, calculates the difference between them (argument1-argument2) and returns the result. Both arguments of this function must be of same type (either time or date-time) .
Following is the syntax of the above function –
TIMEDIFF(expr1, expr2)

Following example demonstrates the usage of the TIMEDIFF() function –
mysql> SELECT TIMEDIFF('21:26;12', '10:40:25');
+----------------------------------+
| TIMEDIFF('21:26;12', '10:40:25') |
+----------------------------------+
| 10:45:35                         |
+----------------------------------+
1 row in set, 1 warning (0.03 sec)
Following is another example of this function –
mysql> SELECT TIMEDIFF('02:45;55', '17:29:45');
+----------------------------------+
| TIMEDIFF('02:45;55', '17:29:45') |
+----------------------------------+
| -14:44:45                        |
+----------------------------------+
1 row in set, 1 warning (0.00 sec)
In the following example we are passing DATETIME values as an argument to this function– MySQL TIMEDIFF() Function
mysql> SELECT TIMEDIFF('2018-05-23 20:40:32', '1996-12-07 14:06:11');
+--------------------------------------------------------+
| TIMEDIFF('2018-05-23 20:40:32', '1996-12-07 14:06:11') |
+--------------------------------------------------------+
| 838:59:59                                              |
+--------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
In the following example we are passing the result of CURTIME() as an argument to the TIMEDIFF() function —
mysql> SELECT TIMEDIFF('20:03:25', CURDATE());
+---------------------------------+
| TIMEDIFF('20:03:25', CURDATE()) |
+---------------------------------+
| -838:59:59                      |
+---------------------------------+
1 row in set, 1 warning (0.00 sec)
We can also pass current timestamp values as arguments to this function –
mysql> SELECT TIMEDIFF(NOW(), '15:09:05 22:58:45');
+--------------------------------------+
| TIMEDIFF(NOW(), '15:09:05 22:58:45') |
+--------------------------------------+
| 838:59:59                            |
+--------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> SELECT TIMEDIFF(CURRENT_TIMESTAMP(), '2015:06:26 00:56:12');
+------------------------------------------------------+
| TIMEDIFF(CURRENT_TIMESTAMP(), '2015:06:26 00:56:12') |
+------------------------------------------------------+
| 838:59:59                                            |
+------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
Let us create another table with name Sales in MySQL database using CREATE statement as follows –
mysql> CREATE TABLE sales(
	ID INT,
	ProductName VARCHAR(255),
	CustomerName VARCHAR(255),
	DispatchDate date,
	DispatchTime time,
	Price INT,
	Location VARCHAR(255)
);
Query OK, 0 rows affected (2.22 sec)
Now, we will insert 5 records in Sales table using INSERT statements −
insert into sales values (1, 'Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad');
insert into sales values (2, 'Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam');
insert into sales values (3, 'Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada');
insert into sales values (4, 'Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai');
insert into sales values (5, 'Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');
Following is another example of this function —
mysql> SELECT ProductName, CustomerName, DispatchDate, Price, DATEDIFF(CURRENT_DATE, DispatchDate) as difference_in_days, TIMEDIFF(CURTIME(), DispatchTime) as time_difference FROM sales;
+-------------+--------------+--------------+-------+--------------------+-----------------+
| ProductName | CustomerName | DispatchDate | Price | difference_in_days | time_difference |
+-------------+--------------+--------------+-------+--------------------+-----------------+
| Key-Board   | Raja         | 2019-09-01   | 7000  | 679                | 06:52:52        |
| Earphones   | Roja         | 2019-05-01   | 2000  | 802                | 06:52:52        |
| Mouse       | Puja         | 2019-03-01   | 3000  | 863                | 06:52:53        |
| Mobile      | Vanaja       | 2019-03-01   | 9000  | 863                | 07:42:00        |
| Headset     | Jalaja       | 2019-04-06   | 6000  | 827                | 06:43:53        |
+-------------+--------------+--------------+-------+--------------------+-----------------+
5 rows in set (0.00 sec)
Suppose we have created a table named SubscribersData with 5 records in it using the following queries –
mysql> CREATE TABLE SubscribersData(
	SubscriberName VARCHAR(255),
	PackageName VARCHAR(255),
	SubscriptionDate date,
	SubscriptionTime time
);
insert into SubscribersData values('Raja', 'Premium', Date('2020-10-21'), Time('20:53:49'));
insert into SubscribersData values('Roja', 'Basic', Date('2020-11-26'), Time('10:13:19'));
insert into SubscribersData values('Puja', 'Moderate', Date('2021-03-07'), Time('05:43:20'));
insert into SubscribersData values('Vanaja', 'Basic', Date('2021-02-21'), Time('16:36:39'));
insert into SubscribersData values('Jalaja', 'Premium', Date('2021-01-30'), Time('12:45:45'));
Following query calculates and displays the remaining number of days and time for the subscription to complete —
mysql> SELECT SubscriberName, PackageName, DATEDIFF(CURRENT_DATE, SubscriptionDate) as RemainingDays, TIMEDIFF(CURTIME(), SubscriptionTime) as RemainingTime FROM SubscribersData;
+----------------+-------------+---------------+---------------+
| SubscriberName | PackageName | RemainingDays | RemainingTime |
+----------------+-------------+---------------+---------------+
| Raja           | Premium     | 263           | -03:00:17     |
| Roja           | Basic       | 227           | 07:40:13      | 
| Puja           | Moderate    | 126           | 12:10:12      |
| Vanaja         | Basic       | 140           | 01:16:53      |
| Jalaja         | Premium     | 162           | 05:07:47      |
+----------------+-------------+---------------+---------------+
5 rows in set (0.05 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page