MySQL - TIMESTAMPDIFF() 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 TIMESTAMPDIFF() function accepts two datetime or date expressions as parameters, calculates the difference between them and returns the result. One of the arguments can be date and the other a datetime expression.
Following is the syntax of the above function –
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

Where,
unit is the interval type represented by the expr value which can be DAY, WEEK, MONTH, QUARTER, YEAR, HOUR, MINUTE, SECOND, MICROSECOND.
unit is the interval type represented by the expr value which can be DAY, WEEK, MONTH, QUARTER, YEAR, HOUR, MINUTE, SECOND, MICROSECOND.
The unit can be mixed values as: SECOND_MICROSECOND, MINUTE_MICROSECOND, MINUTE_SECOND, HOUR_MICROSECOND, HOUR_SECOND, HOUR_MINUTE, DAY_MICROSECOND, DAY_SECOND, DAY_MINUTE, DAY_HOUR, YEAR_MONTH.
datetime_expr1 and datetime_expr2 are the date or date-time expressions.
datetime_expr1 and datetime_expr2 are the date or date-time expressions.
Following example demonstrates the usage of the TIMESTAMPDIFF() function –
mysql> SELECT TIMESTAMPDIFF(DAY, '1898-03-22', '2019-05-02');
+------------------------------------------------+
| TIMESTAMPDIFF(DAY, '1898-03-22', '2019-05-02') |
+------------------------------------------------+
|                                          44235 |
+------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT TIMESTAMPDIFF(MONTH, '1898-03-22', '2019-05-02');
+--------------------------------------------------+
| TIMESTAMPDIFF(MONTH, '1898-03-22', '2019-05-02') |
+--------------------------------------------------+
|                                             1453 |
+--------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT TIMESTAMPDIFF(WEEK, '1898-03-22', '2019-05-02');
+-------------------------------------------------+
| TIMESTAMPDIFF(WEEK, '1898-03-22', '2019-05-02') |
+-------------------------------------------------+
|                                            6319 |
+-------------------------------------------------+
1 row in set (0.00 sec)
Following is another example of this function –
mysql> SELECT TIMESTAMPDIFF(QUARTER, '2021-11-29', '1996-11-22');
+----------------------------------------------------+
| TIMESTAMPDIFF(QUARTER, '2021-11-29', '1996-11-22') |
+----------------------------------------------------+
|                                               -100 |
+----------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPDIFF(YEAR, '2021-11-29', '1996-11-22');
+-------------------------------------------------+
| TIMESTAMPDIFF(YEAR, '2021-11-29', '1996-11-22') |
+-------------------------------------------------+
|                                             -25 |
+-------------------------------------------------+
1 row in set (0.00 sec)
In the following example we are passing DATETIME value for date –
mysql> SELECT TIMESTAMPDIFF(HOUR,'1987-3-12 12:35:58','2018-05-23 20:40:32');
+----------------------------------------------------------------+
| TIMESTAMPDIFF(HOUR,'1987-3-12 12:35:58','2018-05-23 20:40:32') |
+----------------------------------------------------------------+
|                                                         273488 |
+----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPDIFF(MINUTE,'1987-3-12 12:35:58','2018-05-23 20:40:32');
+------------------------------------------------------------------+
| TIMESTAMPDIFF(MINUTE,'1987-3-12 12:35:58','2018-05-23 20:40:32') |
+------------------------------------------------------------------+
| 16409284                                                         |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPDIFF(SECOND,'1987-3-12 12:35:58','2018-05-23 20:40:32');
+------------------------------------------------------------------+
| TIMESTAMPDIFF(SECOND,'1987-3-12 12:35:58','2018-05-23 20:40:32') |
+------------------------------------------------------------------+
| 984557074                                                        |
+------------------------------------------------------------------+
1 row in set (0.00 sec)
In the following example we are passing the result of CURDATE() as an argument to the DATEDIFF() function —
mysql> SELECT TIMESTAMPDIFF(DAY, CURDATE(),'2015-09-05');
+--------------------------------------------+
| TIMESTAMPDIFF(DAY, CURDATE(),'2015-09-05') |
+--------------------------------------------+
|                                      -2141 |
+--------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPDIFF(MONTH,'1995-11-15', CURDATE());
+----------------------------------------------+
| TIMESTAMPDIFF(MONTH,'1995-11-15', CURDATE()) |
+----------------------------------------------+
|                                          308 |
+----------------------------------------------+
1 row in set (0.00 sec)
We can also pass current timestamp values as arguments to this function –
mysql> SELECT DATEDIFF(CURRENT_TIMESTAMP(), '2015-09-05 14:58:25');
+------------------------------------------------------+
| DATEDIFF(CURRENT_TIMESTAMP(), '2015-09-05 14:58:25') |
+------------------------------------------------------+
|                                                 2141 |
+------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF('2015-09-05 14:58:25',CURRENT_TIMESTAMP());
+-----------------------------------------------------+
| DATEDIFF('2015-09-05 14:58:25',CURRENT_TIMESTAMP()) |
+-----------------------------------------------------+
|                                               -2141 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
You can also pass the column name as an argument to this function. Let us create a table with name MyPlayers in MySQL database using CREATE statement as shown below –
mysql> CREATE TABLE MyPlayers(
	ID INT,
	First_Name VARCHAR(255),
	Last_Name VARCHAR(255),
	Date_Of_Birth date,
	Place_Of_Birth VARCHAR(255),
	Country VARCHAR(255),
	PRIMARY KEY (ID)
);
Now, we will insert 7 records in MyPlayers table using INSERT statements −
mysql> insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India');
mysql> insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica');
mysql> insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka');
mysql> insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India');
mysql> insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India');
mysql> insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India');
mysql> insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
Following query calculates the age of the players in years —
mysql> SELECT First_Name, Last_Name, Date_Of_Birth, Country, TIMESTAMPDIFF(YEAR, Date_Of_Birth, NOW()) FROM MyPlayers;
+------------+------------+---------------+-------------+-------------------------------------------+
| First_Name | Last_Name  | Date_Of_Birth | Country     | TIMESTAMPDIFF(YEAR, Date_Of_Birth, NOW()) |
+------------+------------+---------------+-------------+-------------------------------------------+
| Shikhar    | Dhawan     | 1981-12-05    | India       |                                        39 |
| Jonathan   | Trott      | 1981-04-22    | SouthAfrica |                                        40 |
| Kumara     | Sangakkara | 1977-10-27    | Srilanka    |                                        43 |
| Virat      | Kohli      | 1988-11-05    | India       |                                        32 |
| Rohit      | Sharma     | 1987-04-30    | India       |                                        34 |
| Ravindra   | Jadeja     | 1988-12-06    | India       |                                        32 |
| James      | Anderson   | 1982-06-30    | England     |                                        39 |
+------------+------------+---------------+-------------+-------------------------------------------+
7 rows in set (0.00 sec)
Suppose we have created a table named dispatches_data with 5 records in it using the following queries –
mysql> CREATE TABLE dispatches_data(
	ProductName VARCHAR(255),
	CustomerName VARCHAR(255),
	DispatchTimeStamp timestamp,
	Price INT,
	Location VARCHAR(255)
);
insert into dispatches_data values('Key-Board', 'Raja', TIMESTAMP('2019-05-04', '15:02:45'), 7000, 'Hyderabad');
insert into dispatches_data values('Earphones', 'Roja', TIMESTAMP('2019-06-26', '14:13:12'), 2000, 'Vishakhapatnam');
insert into dispatches_data values('Mouse', 'Puja', TIMESTAMP('2019-12-07', '07:50:37'), 3000, 'Vijayawada');
insert into dispatches_data values('Mobile', 'Vanaja' , TIMESTAMP ('2018-03-21', '16:00:45'), 9000, 'Chennai');
insert into dispatches_data values('Headset', 'Jalaja' , TIMESTAMP('2018-12-30', '10:49:27'), 6000, 'Goa');
Following query adds 365 days to the dates of the DispatchTimeStamp column —
mysql> SELECT ProductName, CustomerName, DispatchTimeStamp, Price, TIMESTAMPDIFF(DAY,CURRENT_TIMESTAMP,DispatchTimeStamp) FROM dispatches_data;
+-------------+--------------+---------------------+-------+--------------------------------------------------------+
| ProductName | CustomerName | DispatchTimeStamp   | Price | TIMESTAMPDIFF(DAY,CURRENT_TIMESTAMP,DispatchTimeStamp) |
+-------------+--------------+---------------------+-------+--------------------------------------------------------+
| Key-Board   | Raja         | 2019-05-04 15:02:45 | 7000  |                                                   -804 |
| Earphones   | Roja         | 2019-06-26 14:13:12 | 2000  |                                                   -751 |
| Mouse       | Puja         | 2019-12-07 07:50:37 | 3000  |                                                   -587 |
| Mobile      | Vanaja       | 2018-03-21 16:00:45 | 9000  |                                                  -1213 |
| Headset     | Jalaja       | 2018-12-30 10:49:27 | 6000  |                                                   -929 |
+-------------+--------------+---------------------+-------+--------------------------------------------------------+
5 rows in set (0.00 sec)
Suppose we have created a table named Subscribers with 5 records in it using the following queries –
mysql> CREATE TABLE Subscribers(
	SubscriberName VARCHAR(255),
	PackageName VARCHAR(255),
	SubscriptionDate date
);
insert into Subscribers values('Raja', 'Premium', Date('2020-10-21'));
insert into Subscribers values('Roja', 'Basic', Date('2020-11-26'));
insert into Subscribers values('Puja', 'Moderate', Date('2021-03-07'));
insert into Subscribers values('Vanaja', 'Basic', Date('2021-02-21'));
insert into Subscribers values('Jalaja', 'Premium', Date('2021-01-30'));
Following query calculates and displays the remaining number of days for the subscription to complete —
mysql> SELECT SubscriberName, PackageName, SubscriptionDate, TIMESTAMPDIFF(DAY,SubscriptionDate,CURDATE()) as Remaining_Days FROM Subscribers;
+----------------+-------------+------------------+----------------+
| SubscriberName | PackageName | SubscriptionDate | Remaining_Days |
+----------------+-------------+------------------+----------------+
| Raja           | Premium     | 2020-10-21       |            268 |
| Roja           | Basic       | 2020-11-26       |            232 |
| Puja           | Moderate    | 2021-03-07       |            131 |
| Vanaja         | Basic       | 2021-02-21       |            145 |
| Jalaja         | Premium     | 2021-01-30       |            167 |
+----------------+-------------+------------------+----------------+
5 rows in set (0.00 sec)
Following example demonstrates the usage WEEK and QUARTER units available in the TIMESTAMPDIFF() function –
mysql> SELECT TIMESTAMPDIFF(WEEK,'1920-07-12','2021-03-22');
+-----------------------------------------------+
| TIMESTAMPDIFF(WEEK,'1920-07-12','2021-03-22') |
+-----------------------------------------------+
|                                          5254 |
+-----------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPDIFF(QUARTER,'1969-10-23','2021-03-22');
+--------------------------------------------------+
| TIMESTAMPDIFF(QUARTER,'1969-10-23','2021-03-22') |
+--------------------------------------------------+
|                                              205 |
+--------------------------------------------------+
1 row in set (0.00 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