4

is there a built in functionality in php to convert seconds to hh:mm:ss

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278

4 Answers4

3

There's no built in function, this will work though:

function formatSeconds($seconds){
  $hours = $seconds / (60 * 60);
  $minutes = ( $seconds - $hours * 60 * 60) / 60;
  $seconds = $seconds - $hours * 60 * 60 - $minutes * 60 - $seconds;
  return $hours.':'.$minutes.':'.$seconds;
}
fredley
  • 32,953
  • 42
  • 145
  • 236
0
    $seconds=3672;
    echo date_create()->add(new DateInterval("PT{$seconds}S"))
                      ->diff(new DateTime)
                      ->format('%H:%I:%S');

Just another way that works.

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
-1

I needed an answer to this recently and I ran across this web page concerning inserting a phpdate into a mysql table. (I bet this is also what you're dealing with.) It seems to indicate that you can use something like

$mysqldate = date( 'H:i:s', time());

This seems to work fine even if the number of seconds exceeds a 24 hour period.

JnBrymn
  • 24,245
  • 28
  • 105
  • 147
-2

Yes, the strftime() function (manual):

print(strftime("%H:%M:%S", $_seconds_ ))
david
  • 997
  • 6
  • 15