
- 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
MySQL convert timediff output to day, hour, minute, second format?
To understand the MySQL convert timediff output to day, hour, minute, and second format, you need to use CONCAT() from MySQL.
Let us create a table. The query to create a table is as follows:
mysql> create table convertTimeDifferenceDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> StartDate datetime, -> EndDate datetime, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command. The query to insert record is as follows:
mysql> insert into convertTimeDifferenceDemo(StartDate,EndDate) values(date_add(now(),interval -3 hour),date_add(now(),interval 3 hour)); Query OK, 1 row affected (0.41 sec) mysql> insert into convertTimeDifferenceDemo(StartDate,EndDate) values(date_add(now(),interval -2 hour),date_add(now(),interval 2 hour)); Query OK, 1 row affected (0.27 sec) mysql> insert into convertTimeDifferenceDemo(StartDate,EndDate) values('2018-04-05 12:30:35','2018-05-17 14:30:50'); Query OK, 1 row affected (0.14 sec) mysql> insert into convertTimeDifferenceDemo(StartDate,EndDate) values('2017-10-11 11:20:30','2017-12-17 15:21:55'); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from convertTimeDifferenceDemo;
The following is the output:
+----+---------------------+---------------------+ | Id | StartDate | EndDate | +----+---------------------+---------------------+ | 1 | 2019-01-28 20:55:33 | 2019-01-29 02:55:33 | | 2 | 2019-01-28 21:57:42 | 2019-01-29 01:57:42 | | 3 | 2018-04-05 12:30:35 | 2018-05-17 14:30:50 | | 4 | 2017-10-11 11:20:30 | 2017-12-17 15:21:55 | +----+---------------------+---------------------+ 4 rows in set (0.00 sec)
Here is the query to get timediff output to day, hour, minute, and second format:
mysql> SELECT CONCAT( -> FLOOR(HOUR(TIMEDIFF(StartDate,EndDate)) / 24), ' DAYS ', -> MOD(HOUR(TIMEDIFF(StartDate,EndDate)), 24), ' HOURS ', -> MINUTE(TIMEDIFF(StartDate,EndDate)), ' MINUTES ') AS DESCRIPTION -> FROM convertTimeDifferenceDemo;
The following is the output:
+------------------------------+ | DESCRIPTION | +------------------------------+ | 0 DAYS 6 HOURS 0 MINUTES | | 0 DAYS 4 HOURS 0 MINUTES | | 34 DAYS 22 HOURS 59 MINUTES | | 34 DAYS 22 HOURS 59 MINUTES | +------------------------------+ 4 rows in set, 6 warnings (0.04 sec)
- Related Articles
- MySQL query to convert timediff() to seconds?
- Format MySQL date and convert to year-month-day
- How to convert string to 24-hour datetime format in MySQL?
- Python program to convert time from 12 hour to 24 hour format
- C++ program to convert time from 12 hour to 24 hour format
- C# program to convert time from 12 hour to 24 hour format
- Extract day, hour, minute, etc. from a datetime column in PostgreSQL?
- Convert time from 24 hour clock to 12 hour clock format in C++
- Program to convert hour minutes’ time to text format in Python
- Convert MySQL Unix-Timestamp format to date format?
- How to convert command output to the Hashtable format in PowerShell?
- How to Convert Time Format from 12 Hour to 24 Hour and Vice Versa in Excel?
- Converting 12 hour format time to 24 hour format in JavaScript
- Convert output of MySQL query to UTF8?
- What happens if the output of MySQL TIMEDIFF() function surpass the range value of TIME field?

Advertisements