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
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
Here it is a simple 8-lines PHP function that converts a number of seconds into a human readable string including number of months for large amounts of seconds:
PHP function seconds2human()
This can be achieved with
DateTime
classUse:
Function:
demo
an extended version of Glavić's excellent solution , having integer validation, solving the 1 s problem, and additional support for years and months, at the expense of being less computer parsing friendly in favor of being more human friendly:
var_dump(secondsToHumanReadable(1*60*60*2+1));
->string(16) "2 hours 1 second"
Result will be 19 23:41:07. When it is just one second more than normal day, it is increasing the day value for 1 day. This is why it show 19. You can explode the result for your needs and fix this.
Solution that should exclude 0 values and set correct singular/plural values
Interval class I have written can be used. It can be used in opposite way too.
More info here https://github.com/LubosRemplik/CakePHP-Interval