3

I would like some help converting seconds into a MM:DD:HH:MM:SS format.

I have this code here:

<?php
    // Convert seconds into months, days, hours, minutes, and seconds.
    function secondsToTime($ss) {
    $s = $ss%60;
    $m = floor(($ss%3600)/60);
    $h = floor(($ss%86400)/3600);
    $d = floor(($ss%2592000)/86400);
    $M = floor($ss/2592000);

    return "$M months, $d days, $h hours, $m minutes, $s seconds";
    }
?>

It outputs an example of

0 months, 1 days, 3 hours, 46 minutes, 39 seconds

I would just like it to be something like

00:01:03:46:39

How can I do that?

CSᵠ
  • 10,049
  • 9
  • 41
  • 64
Wesley
  • 57
  • 2
  • 7
  • 5
    use [date()](http://php.net/manual/en/function.date.php) instead ? – nice ass Feb 06 '13 at 04:18
  • what is $ss you are passing in ? integer(timestamp) ? – swapnesh Feb 06 '13 at 04:18
  • Doing as @OneTrickPony suggests or using [`strftime()`](http://php.net/manual/en/function.strftime.php) should make it much easier for you. No need to reinvent the wheel. – inhan Feb 06 '13 at 04:22
  • is there a good reason why you are redoing this? @OneTrickPony has a good solution unless you do. – BOMEz Feb 06 '13 at 04:23
  • I'm using an integer pulled from the DB across multiple rows using a while statement. It's for a rankings by play time page. – Wesley Feb 06 '13 at 04:55
  • Why don't you just removes the `months, days, hours, minutes, seconds` from your code and change them with `:`? – Ari Dec 17 '13 at 14:45

3 Answers3

4

From what I gather, it looks like you're interested in returning a string with values containing two digits separaed by colons. Assuming the positioning doesn't need to change, you can do something similar to the following:

<?php
// Prefix single-digit values with a zero.
function ensure2Digit($number) {
    if($number < 10) {
        $number = '0' . $number;
    }
    return $number;
}

// Convert seconds into months, days, hours, minutes, and seconds.
function secondsToTime($ss) {
    $s = ensure2Digit($ss%60);
    $m = ensure2Digit(floor(($ss%3600)/60));
    $h = ensure2Digit(floor(($ss%86400)/3600));
    $d = ensure2Digit(floor(($ss%2592000)/86400));
    $M = ensure2Digit(floor($ss/2592000));

    return "$M:$d:$h:$m:$s";
}

Or if you don't like the thought of having one more function to manage, perhaps this may suit you better:

<?php
// Convert seconds into months, days, hours, minutes, and seconds.
function secondsToTime($ss) {
    $s = $ss%60;
    $m = floor(($ss%3600)/60);
    $h = floor(($ss%86400)/3600);
    $d = floor(($ss%2592000)/86400);
    $M = floor($ss/2592000);

    // Ensure all values are 2 digits, prepending zero if necessary.
    $s = $s < 10 ? '0' . $s : $s;
    $m = $m < 10 ? '0' . $m : $m;
    $h = $h < 10 ? '0' . $h : $h;
    $d = $d < 10 ? '0' . $d : $d;
    $M = $M < 10 ? '0' . $M : $M;

    return "$M:$d:$h:$m:$s";
}

And then to call our function (whichever method you decide to use):

<?php

$ss = 123456;
print secondsToTime($ss);

?>
Joshua Burns
  • 8,268
  • 4
  • 48
  • 61
2

The easy way:

print date('m:d:h:i:s', $ss); // $ss = seconds

This however can't handle years the way you want it as far as I know, because it will start counting from 1970. But apparently you don't need to display years.

Alternatively, you could just format your current string with (s)printf:

printf('%02d:%02d:%02d:%02d:%02d', $M, $d, $h, $m, $s);
nice ass
  • 16,471
  • 7
  • 50
  • 89
  • I'm using a while statement to get the seconds from multiple rows. Printf and print both set the result at the top of my table for some reason. – Wesley Feb 06 '13 at 04:53
  • That's because `printf` prints the string when its called. Use `sprintf` if you want to return it in your function, and print it later – nice ass Feb 06 '13 at 04:55
  • I tried sprintf as you said, but the fetched data wouldn't even show up. – Wesley Feb 06 '13 at 06:10
  • You needed to replace the `return` line from your function with `return sprintf ...` – nice ass Feb 06 '13 at 06:25
0

Given all your variables, you can simply build the string with PHP:

$str = "$M:$d:$h:$m:$s"

This code wont handle the leading zero's however.

parker.sikand
  • 1,371
  • 2
  • 15
  • 32