17

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!

HMFlol
  • 211
  • 2
  • 4
  • 9
  • In what format is that "decimal"? – Alex Turpin Feb 01 '12 at 20:20
  • what you have doesn't seem right also. Can you provide a sample input and expected output ? – Sorin Feb 01 '12 at 20:22
  • The decimal is nothing special. Something like 5.67891234. – HMFlol Feb 01 '12 at 20:23
  • possible duplicate of [How to Convert decimal number to time or vice versa](http://stackoverflow.com/questions/1345599/how-to-convert-decimal-number-to-time-or-vice-versa) – Gordon Feb 01 '12 at 20:24
  • Also, the output is expected to just be a number for hour, a number for min, and a number for sec that I can then use to print a HTML page. – HMFlol Feb 01 '12 at 20:24
  • But what does that number represent? Hours? – Alex Turpin Feb 01 '12 at 20:24
  • the possible duplicate is tagged c# but there is an answer with the general formula for converting decimal time to regular – Gordon Feb 01 '12 at 20:24
  • Sorry. Yes the number represents hours. And this is not a duplicate. The post you linked to converts to hours and minutes, which I already do. I was having issue with the getting seconds. – HMFlol Feb 01 '12 at 20:26

5 Answers5

26

If $dec is in hours ($dec since the asker specifically mentioned a decimal):

function convertTime($dec)
{
    // start by converting to seconds
    $seconds = ($dec * 3600);
    // we're given hours, so let's get those the easy way
    $hours = floor($dec);
    // since we've "calculated" hours, let's remove them from the seconds variable
    $seconds -= $hours * 3600;
    // calculate minutes left
    $minutes = floor($seconds / 60);
    // remove those from seconds as well
    $seconds -= $minutes * 60;
    // return the time formatted HH:MM:SS
    return lz($hours).":".lz($minutes).":".lz($seconds);
}

// lz = leading zero
function lz($num)
{
    return (strlen($num) < 2) ? "0{$num}" : $num;
}
WWW
  • 9,734
  • 1
  • 29
  • 33
  • Thanks for the help. That makes more sense than what I was trying to do. :) – HMFlol Feb 01 '12 at 20:43
  • 4
    Instead of creating the `lz` function, you can use the native `str_pad` function, from PHP: `str_pad($num, 2, 0, STR_PAD_LEFT)` – elboletaire Sep 08 '13 at 18:53
  • @elboletaire Very true, I just happened to rip this out of some old, old class of mine. – WWW Sep 09 '13 at 13:41
  • Just a note: I don't agree with the community edit made adding the explicit `(int)` cast to that first line of the `convertTime()` function. The edit note said something about casting `$dec` as an `(int)` for rounding - if you want to round off the seconds, just use `round()` in the appropriate place. – WWW Jan 21 '14 at 15:44
  • 2
    In fact, since the asker specifically asked about a *decimal*, I removed the `(int)` cast. Doesn't make much sense to truncate the hours variable and yet still do minute and second calculations (since if you round the hours variable, there won't **be** any minutes or seconds!) – WWW Jun 25 '15 at 17:00
16

Very simple solution in one line:

echo gmdate('H:i:s', floor(5.67891234 * 3600));
Cheery
  • 16,063
  • 42
  • 57
5

Everything upvoted didnt work in my case. I have used that solution to convert decimal hours and minutes to normal time format. i.e.

function clockalize($in){

    $h = intval($in);
    $m = round((((($in - $h) / 100.0) * 60.0) * 100), 0);
    if ($m == 60)
    {
        $h++;
        $m = 0;
    }
    $retval = sprintf("%02d:%02d", $h, $m);
    return $retval;
}


clockalize("17.5"); // 17:30
Patrick Jaja
  • 96
  • 1
  • 3
0

This is a great way and avoids problems with floating point precision:

function convertTime($h) {
    return [floor($h), (floor($h * 60) % 60), floor($h * 3600) % 60];
}
Teldan
  • 1
0

I am not sure if this is the best way to do this, but

$variabletocutcomputation = 60 * ($dec - $hour);
$min = round($variabletocutcomputation);
$sec = round((60*($variabletocutcomputation - $min)));
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180