1

I am currently trying to get a countdown clock using a unix timestamp value (remaining seconds) to H:M:S format, in order to sync my client side timers.

The value that I need to change to H:M:S has already been calculated as the time remaining in the countdown.

Let

$remaining_time

be our value of remaining seconds in the countdown.

Currently, here is what I have:

$H = floor($remaining_time / 3600);
$M = floor(($remaining_time - ($H*3600)) / 60);
$S = floor($remaining_time - ($H*3600 - ($M*60)));

I believe the hours/minutes is pretty close... but the seconds seems to be off. For example, I am getting results like this

$remaining_time = 4135;

Result: Time Remaining at price formatted: H:1 M:8 S:1015

Any information is appreciated - again, I need the remaining seconds in hours, minutes, and seconds.

Alpinestar22
  • 553
  • 2
  • 9
  • 20
  • possible duplicate of [How to convert milliseconds into human readable form?](http://stackoverflow.com/questions/175554/how-to-convert-milliseconds-into-human-readable-form) – Greg Hewgill May 28 '13 at 05:03

1 Answers1

3

Use gmdate, example:

<?php

$remaining_time = 4135;

echo "Time Remaining at price formatted: ".gmdate("\H:H \M:i \S:s",4135);

//Result: Time Remaining at price formatted: H:01 M:08 S:55
?>
Dave Chen
  • 10,887
  • 8
  • 39
  • 67