0

This should be easy...

I have a duration in seconds, I want to output it in hours:minutes:seconds format

When I try...

// video duration is 1560 seconds

<?=date("h:i:s",$video->duration)?>

...it outputs 07:26:00 instead of 00:26:00. So I figure it's a time zone issue.

At the beginning of my application I set the timezone like this

date_default_timezone_set('America/Montreal');

Now I guess I would get my normal value if I change the timezone to GMT but I don't want to do that each time I want a simple duration (and re-set the timezone to America/Montreal)

What are my options?

Guillaume Bois
  • 1,302
  • 3
  • 15
  • 23
  • possible duplicate of [Timezone conversion in php](http://stackoverflow.com/questions/2505681/timezone-conversion-in-php) – Gordon Dec 06 '11 at 14:20
  • 1
    `$dt = new DateTime('@1560'); $dt->setTimezone(new DateTimeZone('UTC')); echo $dt->format('H:i:s');` Note this will never show any time beyond 23:59:59 because this would be the next day then and the output would start at 00:00:00 then. – Gordon Dec 06 '11 at 14:21
  • There is a number of additional possible duplicate dealing with duration at http://stackoverflow.com/search?q=duration+php as well – Gordon Dec 06 '11 at 14:29
  • @Gordon that is exactly what I needed. I didn't know about the "@" thick... not easy to find. this avoid having to use a helper function. Great! – Guillaume Bois Dec 06 '11 at 16:31

2 Answers2

1

date is used to get a human readable format for an unix timestamp, so it's not what you need. In other words that function is used to show a date (like 12.12.2012) and not the duration of your video

for that you can create a function that starts from the total number of seconds and does something like

$seconds = $total_time %60;
$minutes = (floor($total_time/60)) % 60;
$hours = floor($total_time/3600);
return $hours . ':' . $minutes . ':' . $seconds;

maybe you need to adjust this a bit, as I did not test it. also some tests to skip hours or minutes if the video is short

mishu
  • 5,347
  • 1
  • 21
  • 39
0

This is an old question but I thought I can provide an answer for this:

function convert_to_duration($total_time){
    $seconds = $total_time %60;
    $minutes = (floor($total_time/60)) % 60;
    $hours = floor($total_time/3600);
    if($hours == 0){
        return $minutes . ':' . sprintf('%02d', $seconds);
    }else{
        return $hours . ':' . $minutes . ':' . sprintf('%02d', $seconds);
    }
}
syrkull
  • 2,295
  • 4
  • 35
  • 68