6

Possible Duplicate:
Convert seconds to Hour:Minute:Second

I've been searching all over the internet to find a good way of converting seconds into minutes:seconds without leading zeros. I already checked out this question, which is the only one I was even able to find, however none of those answers look very good. Perhaps they are the only and best way to achieve this, however I would hope not.

I have done this and it gives me the number of minutes without the leading zeros, however I am unable to get the seconds. The only way I can think of doing it this way would be to do a few lines of math and such, but that seems like an awful lot of work for something as simple as this... which I don't know why PHP doesn't have it built in for minutes and seconds anyways....

intval(gmdate("i:s", $duration));

Edit All I am trying to do is to convert the number of seconds in a video, into a H:M:S format.

Community
  • 1
  • 1
Dylan Cross
  • 5,918
  • 22
  • 77
  • 118
  • What do you need `intval()` for? **Why** did you add it? – zerkms Dec 27 '12 at 03:04
  • Well, it is one of the answers on the other overflow question in my question, it removes the zeros, and everything else as well.. – Dylan Cross Dec 27 '12 at 03:05
  • no it doesn't. Please read documentation before you apply functions randomly: http://php.net/intval – zerkms Dec 27 '12 at 03:06
  • Like I said, I didn't apply it myself, it was one of the few answers on the only topic of this, and it was suggested this way a couple of times if I'm not mistaken. Also, I was just stating what it did do to my date, which it did remove the zeros and non numerical values. – Dylan Cross Dec 27 '12 at 03:09
  • "Like I said, I didn't apply it myself, it was one of the few answers on the only topic" --- even worse. It's a community of programmers, not code-copy-pasters. – zerkms Dec 27 '12 at 03:09
  • This isn't a duplicate of that post, I want to remove leading zeros.. that one doesn't..... – Dylan Cross Dec 27 '12 at 03:10

3 Answers3

2
implode(
    ':',
    array_map(
        function($i) { return intval($i, 10); },
        explode(':', gmdate('H:i:s', $duration))
    )
)

however what about if hour==0 then do not print 0: and just have m:s

preg_replace(
    '~^0:~',
    '', 
    implode(
        ':',
        array_map(
            function($i) { return intval($i, 10); },
            explode(':', gmdate('H:i:s', $duration))
        )
    )
)
zerkms
  • 249,484
  • 69
  • 436
  • 539
2

I would just write it iteratively:

function duration_to_timestring($duration)
{
        $s = [];
        if ($duration > 3600) {
                $s[] = floor($duration / 3600);
                $duration %= 3600;
        }
        $s[] = floor($duration / 60);
        $s[] = floor($duration % 60);

        return join(':', $s);
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • This would be good too, although I still can't figure out why php doesn't include minutes and seconds without leading zeros. – Dylan Cross Dec 27 '12 at 03:35
  • 1
    @DylanCross Only the privileged know why, perhaps they ran out of useful letters to use as format specifiers ;-) – Ja͢ck Dec 27 '12 at 03:45
1

gmdate takes a timestamp as the second parameter. You should do something like this:

echo gmdate("H:i:s", mktime(0, 0, 0, 1, 1, 1998) + $duration);

intval should not be there, as you are getting a string and are transforming it to an int again. With H:i:s you have 10:40:05.
This will however not work if you have the duration > then 24h.

Mihai P.
  • 9,307
  • 3
  • 38
  • 49
  • I want to remove the leading zeros though, the code above doesn't. – Dylan Cross Dec 27 '12 at 03:11
  • If you want without the leading 0 then you will have to use $hours = date("h", mktime(0, 0, 0, 1, 1, 1998) + $duration); $minutes = intval(date("i", mktime(0, 0, 0, 1, 1, 1998) + $duration)); $seconds = intval(date("s", mktime(0, 0, 0, 1, 1, 1998) + $duration)); – Mihai P. Dec 27 '12 at 03:12
  • I edited my code, I have a feeling that you should use date instead of gmdate. Otherwise now might not be accurate depending on the server date. – Mihai P. Dec 27 '12 at 03:13
  • Thanks, the only problem is that the Hour one, when it should be zero, is now 12, but before you edited it, it was 5. – Dylan Cross Dec 27 '12 at 03:17
  • you are right it should be $hours = date("G", mktime(0, 0, 0, 1, 1, 1998) + $duration); – Mihai P. Dec 27 '12 at 03:23