Skip to content

Instantly share code, notes, and snippets.

@jgornick
Created May 13, 2013 15:24
Show Gist options
  • Save jgornick/5569137 to your computer and use it in GitHub Desktop.
Save jgornick/5569137 to your computer and use it in GitHub Desktop.
PHP: Convert seconds to hours:minutes:seconds.
<?php
$diff = 450752;
$format = sprintf('%02d:%02d:%02d', ($diff / 3600), ($diff / 60 % 60), $diff % 60);
echo $format;
@dejurin
Copy link

dejurin commented Aug 6, 2015

$h_ = (($diff / 3600) >= 1) ? '%02d:':'';
$m_ = (($diff / 60 % 60) >= 1) ? '%02d:':'';
$format = sprintf($h_.$m_.'%02d', ($diff / 3600), ($diff / 60 % 60), $diff % 60);
return $format;

if hour or minute = 0 not need show 00.

@wdog
Copy link

wdog commented Jul 20, 2016

why not

gmdate('H:i:s', $seconds)

@sn1pah
Copy link

sn1pah commented Aug 10, 2016

Cose it works only to 24h?

@NickSun
Copy link

NickSun commented Mar 3, 2017

sprintf('%02d',floor($seconds / 3600)).gmdate(":i:s", $seconds % 3600);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment