Home > Database > Mysql Tutorial > How to Convert Time Difference Output to Day, Hour, Minute, and Second Format in MySQL?

How to Convert Time Difference Output to Day, Hour, Minute, and Second Format in MySQL?

Patricia Arquette
Release: 2024-10-24 04:14:02
Original
650 people have browsed it

How to Convert Time Difference Output to Day, Hour, Minute, and Second Format in MySQL?

Converting Time Difference Output to Day, Hour, Minute, and Second Format in MySQL

When working with time differences in MySQL, it's often useful to display the result in a more human-readable format, such as days, hours, minutes, and seconds. This can be challenging if the time difference is large.

Suppose you have a query like:

SELECT TIMEDIFF(end_time,start_time) AS "total" FROM `metrics`;
Copy after login

which returns a value like:

116:12:10
Copy after login

representing 116 hours, 12 minutes, and 10 seconds. To convert this to a more readable format, use the following query:

SELECT CONCAT(
FLOOR(HOUR(TIMEDIFF(end_time,start_time)) / 24), ' days ',
MOD(HOUR(TIMEDIFF(end_time,start_time)), 24), ' hours ',
MINUTE(TIMEDIFF(end_time,start_time)), ' minutes')
Copy after login

Replace end_time and start_time with your actual column names.

For example, let's say end_time is '2010-01-06 08:46' and start_time is '2010-01-01 12:30'. The above query would return:

4 days 20 hours 12 minutes
Copy after login

Note that this solution only works for date differences within 35 days. If you know there are more than 35 days between start_time and end_time, consider using TIMESTAMPDIFF instead.

The above is the detailed content of How to Convert Time Difference Output to Day, Hour, Minute, and Second Format in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template