
- 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
Find the difference between two timestamps in days with MySQL
Use DATEDIFF() function from MySQL to get the difference between two timestamps in days.
The syntax is as follows −
select datediff(yourColumnName1,yourColumnName2) as anyVariableName from yourTableName;
To understand the above syntax, let us create a table. The following is the query to create a table −
mysql> create table DifferenceTimestamp −> ( −> IssueTime timestamp, −> DueTime timestamp −> ); Query OK, 0 rows affected (0.66 sec)
Insert some records in the table with the help of insert command. We are setting dates here. The query is as follows −
mysql> insert into DifferenceTimestamp values(now(),date_add(now(),interval -30 Day)); Query OK, 1 row affected (0.12 sec) mysql> insert into DifferenceTimestamp values(now(),date_add(now(),interval -24 Day)); Query OK, 1 row affected (0.16 sec) mysql> insert into DifferenceTimestamp values(now(),date_add(now(),interval -5 Day)); Query OK, 1 row affected (0.14 sec)
Display all records from the table with the help of select statement. The query is as follows −
mysql> select *from DifferenceTimestamp;
The following is the output −
+---------------------+---------------------+ | IssueTime | DueTime | +---------------------+---------------------+ | 2018-12-07 17:48:28 | 2018-11-07 17:48:28 | | 2018-12-07 17:48:40 | 2018-11-13 17:48:40 | | 2018-12-07 17:48:46 | 2018-12-02 17:48:46 | +---------------------+---------------------+ 3 rows in set (0.00 sec)
Here is the query to get difference between two timestamps in days. The query is as follows −
mysql> SELECT DATEDIFF(IssueTime, DueTime) AS DifferenceInTimestampDays from DifferenceTimestamp;
The following is the output −
+---------------------------+ | DifferenceInTimestampDays | +---------------------------+ | 30 | | 24 | | 5 | +---------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL difference between two timestamps in Seconds?
- Difference between two timestamps in seconds in MySQL?
- Get the difference between two timestamps in seconds in MySQL?
- Python program to find difference between two timestamps
- What is the difference between UNIX TIMESTAMPS and MySQL TIMESTAMPS?
- Find the difference between two datetime values with MySQL?
- How to get time difference between two timestamps in seconds?
- What is the way to find business days between two specified dates in MySQL?
- How to get the duration between two Instant timestamps in Java
- How to find absolute difference between two numbers in MySQL?
- Difference between two selects in MySQL?
- Calculate the difference between two dates in days, weeks, months and years in Excel
- MySQL query to get next closest day between two days?
- How to find the difference in number of days between two date columns of an R data frame?
- Find the difference between dates in the form of months with MySQL

Advertisements