13

i have tried using gmdate to convert seconds into HH:MM:SS with this code:

gmdate("H:i:s", $result["s"]

$result["s] equals 142:000

when i echo the gmdate code, it displays 00:24:02 which is wrong, it should be 00:02:36

  • In terms of date or elapsed time? – bcmcfc Dec 19 '13 at 09:17
  • 1
    Why is it `142:000`? `gmdate` take a timestamp, not seconds from nowhere. – casraf Dec 19 '13 at 09:17
  • its in terms of elapsed time –  Dec 19 '13 at 09:18
  • `142:000` = 142 is seconds? What is 000 part? Isn't 142 seconds = 2min:22sec, not 2min:36sec like you wrote? – Glavić Dec 19 '13 at 09:19
  • Please read the [documentation](http://en.php.net/function.gmdate) and you will see, that `gmdate()` expects a unix timestamp, not some arbitrary seconds value you came up with. – Till Helge Dec 19 '13 at 09:19
  • 1
    It echos `00:02:22` for me, which is the correct output. (142 - 120 = 22 => 2:22) http://codepad.org/CkH8bwze – casraf Dec 19 '13 at 09:19
  • @TillHelge I agree but it will just assume 142 elapsed seconds from 1.1.1970 @ 00:00:00, which will output the correct time (as long as date isn't in the question) – casraf Dec 19 '13 at 09:20
  • @ChenAsraf where is the 120 coming from? 142 (or 142.000) seconds equals 2 minutes 36 seconds (142 / 60) - http://bit.ly/18Aozei - but its showing 00:02:22 –  Dec 19 '13 at 09:40
  • 120 is 60 * 2 = 2 minutes. The remainder (22) is the remaining seconds – casraf Dec 19 '13 at 09:40
  • but 142 seconds in minutes is 02:36 –  Dec 19 '13 at 09:45
  • check here http://bit.ly/18Aozei –  Dec 19 '13 at 09:48
  • 1
    @charliejsford: you link is correct, that is `2.36666` **MINUTES**, where do you see on that link `2min:36sec`? See my answer bellow for explanation. – Glavić Dec 19 '13 at 09:49

4 Answers4

14

Where is the 120 coming from? 142 (or 142.000) seconds equals 2 minutes 36 seconds (142 / 60)

142 / 60 = 2.36666667 minutes which doesn't equals 2min 36sec. 2.36666 is a decimal number, and it represents minutes. If you wish to format 2.36666 to minutes and seconds, then take the whole number as minutes, and 0.366666 * 60 as seconds, that is 22, so result is 2min 22sec.


You should cast 2nd parameter to integer, or at least remove :000 part:

$result['s'] = '142:000';
echo gmdate("H:i:s", (int)$result['s']); # 0:02:22

demo

You will have problem, if you have more than 86400 seconds (1 day). In that case you can use this.

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
4

Try this

echo gmdate("H:i:s", 685);

OR

days = seconds / ( 24 * 60 * 60 )
seconds -= ( days * ( 24 * 60 * 60 ) )

hours = seconds / ( 60 * 60 )
seconds -= ( hours * ( 60 * 60 ) )

minutes = seconds / 60
seconds -= ( minutes * 60 )
Nisarg
  • 3,024
  • 5
  • 32
  • 54
  • variables need "$" and you can use the modulus operator ! – Mimouni Aug 12 '16 at 10:22
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Aug 12 '16 at 14:20
1

Y0u can try this

$getHours = floor($seconds / 3600);
$getMins = floor(($seconds - ($getHours*3600)) / 60);
$getSecs = floor($seconds % 60);
echo $getHours.':'.$getMins.':'.$getSecs;
Let me see
  • 5,063
  • 9
  • 34
  • 47
1

This works for me. Function format. No leading zeros and unlimited hours, days not used.

 /* WHM MOD convert a string of seconds to H:M:S format */

function sec2hms($secs) {
    $secs = round($secs);
    $secs = abs($secs);
    $hours = floor($secs / 3600) . ':';
    if ($hours == '0:') $hours = '';
    $minutes = substr('00' . floor(($secs / 60) % 60), -2) . ':';
    $seconds = substr('00' . $secs % 60, -2);
return ltrim($hours . $minutes . $seconds, '0');
}
Wolf359
  • 11
  • 2