3

Trying to generate simple "H:i:s" string from seconds, but can't

example:

$iSecond = 188;
$dtF = new DateTime("@0");
$dtT = new DateTime("@$iSecond");
$size = $dtF->diff($dtT)->format('%H:%i:%s');
echo $size;

should show "00:03:08" , but in fact "00:3:8"

if there is some another date format options to use in format method?

there is fiddle https://3v4l.org/Coi7q

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95

2 Answers2

14

Use capital I and S for your formatting:

$size = $dtF->diff($dtT)->format('%H:%I:%S');

See the manual for all of the formatting options

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
4

John is correct, but your code seems unnecessary, this should be the same:

$iSecond = 188;
$dtT = new DateTime("@$iSecond");
$size = $dtT->format('H:i:s');

There is no reason to create a datetime of @0 and use diff/DateInterval in this code.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95