Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert seconds into days, hours, minutes and seconds

Tags:

date

php

I would like to convert a variable $uptime which is seconds, into days, hours, minutes and seconds.

Example:

$uptime = 1640467; 

Result should be:

18 days 23 hours 41 minutes 
like image 547
Florian Avatar asked Nov 25 '11 20:11

Florian


People also ask

How do you convert days seconds to hours minutes and seconds?

int seconds = (totalSeconds % 60); int minutes = (totalSeconds % 3600) / 60; int hours = (totalSeconds % 86400) / 3600; int days = (totalSeconds % (86400 * 30)) / 86400; First line - We get the remainder of seconds when dividing by number of seconds in a minutes.


1 Answers

This can be achieved with DateTime class

Function:

function secondsToTime($seconds) {     $dtF = new \DateTime('@0');     $dtT = new \DateTime("@$seconds");     return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds'); } 

Use:

echo secondsToTime(1640467); # 18 days, 23 hours, 41 minutes and 7 seconds 

demo

like image 192
Glavić Avatar answered Oct 17 '22 11:10

Glavić