
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Get the difference between two timestamps in seconds in MySQL?
To get difference between two timestamps in seconds, use two in-built functions TIME_TO_SEC() and TIMEDIFF() in MySQL. The syntax is as follows −
select time_to_sec(timediff(yourCoulnName1,yourCoulnName2)) as anyVariableName from yourTableName;
To understand the above concept, let us first create a table. The query to create a table.
mysql> create table TimeToSecond −> ( −> MyTime timestamp, −> YourTime timestamp −> ); Query OK, 0 rows affected (0.48 sec)
Now you can insert some datetime values in the table. The query is as follows −
mysql> insert into TimeToSecond values('2016-05-10 10:02:00','2016-05-10 10:00:00'); Query OK, 1 row affected (0.15 sec) mysql> insert into TimeToSecond values('2016-05-10 10:06:00','2016-05-10 10:03:00'); Query OK, 1 row affected (0.24 sec) mysql> insert into TimeToSecond values('2018-05-10 11:00:00','2018-05-10 10:00:00'); Query OK, 1 row affected (0.08 sec)
After inserting, you can check how many records are present in the table with the help of select statement. The query to display all records is as follows −
mysql> select *from TimeToSecond;
The following is the output −
+---------------------+---------------------+ | MyTime | YourTime | +---------------------+---------------------+ | 2016-05-10 10:02:00 | 2016-05-10 10:00:00 | | 2016-05-10 10:06:00 | 2016-05-10 10:03:00 | | 2018-05-10 11:00:00 | 2018-05-10 10:00:00 | +---------------------+---------------------+ 3 rows in set (0.00 sec)
Let us now get the difference between two timestamps in seconds using the syntax we discussed above. The query is as follows −
mysql> select time_to_sec(timediff(MyTime,YourTime)) as DifferenceInSeconds from TimeToSecond;
The following is the output −
+---------------------+ | DifferenceInSeconds | +---------------------+ | 120 | | 180 | | 3600 | +---------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL difference between two timestamps in Seconds?
- Difference between two timestamps in seconds in MySQL?
- How to get time difference between two timestamps in seconds?
- How to get the seconds and minutes between two Instant timestamps in Java
- Find the difference between two timestamps in days with MySQL
- What is the difference between UNIX TIMESTAMPS and MySQL TIMESTAMPS?
- C# Program to get the difference between two dates in seconds
- How to get the duration between two Instant timestamps in Java
- Python program to find difference between two timestamps
- Java Program to get the difference between two time zones by seconds
- How to get the difference between two columns in a new column in MySQL?
- How to get the number of seconds between two Dates in JavaScript?
- Difference between two selects in MySQL?
- Sorted difference between two columns in MySQL?
- How to compare timestamps in MySQL?

Advertisements