我的看法
function formatSeconds( $seconds )
{
$hours = 0;
$milliseconds = str_replace( "0.", '', $seconds - floor( $seconds ) );
if ( $seconds > 3600 )
{
$hours = floor( $seconds / 3600 );
}
$seconds = $seconds % 3600;
return str_pad( $hours, 2, '0', STR_PAD_LEFT )
. gmdate( ':i:s', $seconds )
. ($milliseconds ? ".$milliseconds" : '')
;
}
然后测试
$testData = array(
23, // Seconds, w/o millis
23.75, // Seconds, w/millis
23.75123456789, // Lots of millis
123456789.75 // Lots of seconds
);
foreach ( $testData as $seconds )
{
echo formatSeconds( $seconds ), PHP_EOL;
}
其产生
00:00:23
00:00:23.75
00:00:23.75123456789
34293:33:09.75