Convert seconds to days, hours, minutes, seconds (MySQL)

Zameer Ahmed picture Zameer Ahmed · Dec 26, 2016 · Viewed 10.3k times · Source

Example:

seconds ="1015557";

Result should be:

11days 18h:05m:57s

How can do this in MySQL?

Answer

Gurwinder Singh picture Gurwinder Singh · Dec 26, 2016
select concat(
    format(floor(s / @day),0),
    'days ',
    time_format(sec_to_time(s % @day),'%Hh:%im:%ss') 
  ) formatted_date
from (select 1015557 s) t, (select @day = 3600 * 24);

produces:

+--------------------+                                                                                                                               
| days               |                                                                                                                               
+--------------------+                                                                                                                               
| 11days 18h:05m:57s |                                                                                                                               
+--------------------+