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
There are some very good answers here but none of them covered my needs. I built on Glavic's answer to add some extra features that I needed;
You can see a running version of the code here.
The simplest approach would be to create a method that returns a DateInterval from the DateTime::diff of the relative time in $seconds from the current time $now which you can then chain and format. For example:-
Now chain your method call to DateInterval::format
Result:
This is the function rewritten to include days. I also changed the variable names to make the code easier to understand...
Source: CodeAid() - http://codeaid.net/php/convert-seconds-to-hours-minutes-and-seconds-(php)
Although it is quite old question - one may find these useful (not written to be fast):
result:
This is a function i used in the past for substracting a date from another one related with your question, my principe was to get how many days, hours minutes and seconds has left until a product has expired :