0

I am using gmdate in PHP to convert seconds to H:i:s

I have this code:

echo gmdate("H:i:s", '480002');

So i should be converting 480002 seconds to H:i:s which should show

133:20:02

but its only showing

13:20:02
Charles
  • 179
  • 6
  • 19
  • 1
    The `H` format code is hour in the day (`H - 24-hour format of an hour with leading zeros - 00 through 23 `).... there are only 24 hours in a day, so why would you expect to see 133? – Mark Baker Mar 02 '15 at 12:08
  • I see - so how can i make it show the full, correct amount? – Charles Mar 02 '15 at 12:09

2 Answers2

3

Use this

<?php

$init = 480002;
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;

echo "$hours:$minutes:$seconds";

?>
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
0

gmdate is working with date values not dateinterval values, H in the format string therefore represents the number of hours since midnight. Generally days don't have 133 hours, so this is interpreted as 5 days and 13 hours. You haven't asked it to display the days part, so it just shows 13 hours.

There are various ways to calculate the total number of hours.

I'd suggest looking at this question: Calculate number of hours between 2 dates in PHP

Community
  • 1
  • 1
Phil Preen
  • 589
  • 5
  • 20