Change output time using php

I have a time-average total, which is calculated from the time converted from an integer.

average = 0:0:20

I wanted to change my output as follows:

average = 00:00:20

By the way, this is the code I used to get the average time:

$ans = $times / $displaycount;
$hh = floor($ans / 3600);
$mm = floor(($ans - ($hours*3600)) / 60);
$ss = floor($ans % 60);

$timeavg = $hh.':'.$mm.':'.$ss;

echo "average = ". $timeavg;
+4
source share
2 answers

to try

$str= '0:0:20';
echo date('H:i:s', strtotime($str)); //output :- 00:00:20 
+2
source

I would recommend using a format function, for example sprintf():

$timeavg = sprintf('%02d:%02d:%02d', $hh, $mm, $ss);

demo

0
source

Source: https://habr.com/ru/post/1536723/


All Articles