Build dashboards & reports in minutes

  • Features
  • Pricing
  • Blog
  • What is Ubiq
  • Free Trial

How to find difference between two dates in MySQL

November 29, 2013September 11, 2020 Sreeram Sreenivasan

Last updated on September 11th, 2020 at 11:36 am

Calculating difference between two dates in MySQL is really easy. Here’s how you can do it. Let’s say dt1 and dt2 are 2 columns in table table_name. Just replace db1, db2 and table_name in the queries below with your values.

A simple use case is a table called user with 2 fields called signup_date and last_login_date. You can calculate difference between two dates to find how recently users have logged in after signup.

Table of Contents

Toggle
  • Difference between two dates present as columns in a table
  • Difference between two dates stored as variables
  • Difference between two dates in minutes
  • Difference between two dates in hours
  • Difference between two dates in days

Difference between two dates present as columns in a table

SELECT UNIX_TIMESTAMP( dt2 ) - UNIX_TIMESTAMP( dt1 ) from table_name;

Difference between two dates stored as variables

SET @dt1='2013-11-09 00:00:00';
SET @dt2='2012-10-07 00:00:00';
SELECT UNIX_TIMESTAMP( @dt2 ) - UNIX_TIMESTAMP( @dt1 );

Difference between two dates in minutes

If you want to find out the difference in terms on minutes, just divide the above result by 60

SELECT (UNIX_TIMESTAMP( dt2 ) - UNIX_TIMESTAMP( dt1 ))/60 from table_name;

Difference between two dates in hours

If you want to find out the difference in terms on minutes, just divide the above result by 3600

SELECT (UNIX_TIMESTAMP( dt2 ) - UNIX_TIMESTAMP( dt1 ))/3600 from table_name;

Difference between two dates in days

If you want to find out the difference in terms on minutes, just divide the above result by 3600*24

SELECT (UNIX_TIMESTAMP( dt2 ) - UNIX_TIMESTAMP( dt1 ))/(3600*24) from table_name;

Related posts:

MySQL DROP DATABASE

How to Get Last 3 Months Sales Data in MySQL

MySQL Rollback Query

mm
Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on X (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
MySQL data, database, date, date arithmetic, date difference, date login, date signup, date variable difference, day difference, hour difference, minute difference, multiple dates, mysql, query, second difference, sql, tips, unix timestamp. permalink.

Post navigation

MySQL Tutorial : MySQL IN clause (Basic)
How to calculate age from date of birth in SQL

About Us



Ubiq is a business intelligence & reporting software. Build business dashboards, charts & reports in minutes. Get insights from data quickly. Try it for free!




Data Reporting
  • MySQL Reporting Tools
  • PostgreSQL Reporting Tools
  • Online Reporting Tools
  • Web Reporting Tools
  • Redshift BI Reporting
  • SQL Reporting Tools
Business Intelligence
  • BI Solution
  • BI Reporting Software
  • MySQL BI Reporting Tools
  • Self Service BI
  • SaaS BI
Data Visualization
  • Data Visualization Tools
  • Data Analysis Tools
  • Visual Analytics
  • MySQL Charts
  • MySQL Graph Generator
  • MySQL Report Builder
  • Online Report Generator
  • Redshift Data Visualization
Dashboards
  • Dashboard Builder
  • Dashboard Reporting Software
  • Dashboard Creator
  • KPI Dashboard Software
Quicklinks
  • Contact Us
  • Docs
  • Jobs
  • BI Blog
  • Database Blog
  • Tech Blog
Resources
  • Security
  • Privacy
  • T&C
  • Sitemap
dazzling Theme by Colorlib Powered by WordPress