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
Short, simple, reliable :
Here's some code that I like to use for the purpose of getting the duration between two dates. It accepts two dates and gives you a nice sentence structured reply.
This is a slightly modified version of the code found here.
Source: https://gist.github.com/ozh/8169202
All in one solution. Gives no units with zeroes. Will only produce number of units you specify (3 by default). Quite long, perhaps not very elegant. Defines are optional, but might come in handy in a big project.
Feel free to down-vote, but be sure to try it in your code. It might just be what you need.
With DateInterval :
Based on the answer by Julian Moreno, but changed to give the response as a string (not an array), only include the time intervals required and not assume the plural.
The difference between this and the highest voted answer is:
For
259264
seconds, this code would giveFor
259264
seconds, the highest voted answer (by Glavić) would giveI hope this helps someone.