I am trying to take a decimal and convert it so that I can echo it as hours, minutes, and seconds.
I have the hours and minutes, but am breaking my brain trying to find the seconds. Been googling for awhile with no luck. I'm sure it is quite simple, but nothing I have tried has worked. Any advice is appreciated!
Here is what I have:
function convertTime($dec)
{
$hour = floor($dec);
$min = round(60*($dec - $hour));
}
Like I said, I get the hour and minute without issue. Just struggling to get seconds for some reason.
Thanks!
If
$dec
is in hours ($dec
since the asker specifically mentioned a decimal):Everything upvoted didnt work in my case. I have used that solution to convert decimal hours and minutes to normal time format. i.e.
I am not sure if this is the best way to do this, but
Very simple solution in one line:
This is a great way and avoids problems with floating point precision: