如何在PHP中使用小数秒将十进制秒转换为时间

如何在PHP中使用小数秒将十进制秒转换为时间,php,time,Php,Time,我试图解决一个问题,即我需要能够以秒为单位将时间转换为小数点,例如375.844我需要将其转换为以下格式: HH:MM:SS.0而不是四舍五入到最接近的整秒 我相信上面的数字应该显示为00:06:15.8 这是一个更广泛的程序的一部分,用户可以输入小时、分钟和秒,允许十分之一秒 我使用以下函数将所有这些转换为秒: public function timetosecs() { return $this->hours * (60 * 60) + $this->mins * 60

我试图解决一个问题,即我需要能够以秒为单位将时间转换为小数点,例如
375.844
我需要将其转换为以下格式:
HH:MM:SS.0
而不是四舍五入到最接近的整秒 我相信上面的数字应该显示为
00:06:15.8

这是一个更广泛的程序的一部分,用户可以输入小时、分钟和秒,允许十分之一秒

我使用以下函数将所有这些转换为秒:

public function timetosecs() {
    return $this->hours * (60 * 60) + $this->mins * 60 + $this->secs * 1;
}
然后,它在秒数上执行一些计算以调整它们,乘以系数
0.866
,然后这是将它们转换回时间格式的函数,但它似乎不显示十分之一

  public function secstotime($totalSeconds) {
    $hours = floor($totalSeconds / 3600);
    $totalSeconds %= 3600;
    $minutes = floor($totalSeconds / 60);
    $seconds = floor(($totalSeconds % 60) * 10) / 10;
    return $hours . ":" . $minutes . ":" . $seconds;
}
使用
sprintf()
函数设置前导数字:

function secstotime($totalSeconds) {
    $startTotalSeconds = $totalSeconds;
    $hours = floor($totalSeconds / 3600);
    $totalSeconds %= 3600;
    $minutes = floor($totalSeconds / 60);
    $seconds = $startTotalSeconds - ($minutes * 60);
    return sprintf("%02d",$hours) . ":" . sprintf("%02d",$minutes) . ":" .sprintf("%.1f", $seconds);
}


//...
echo secstotime(375.844); // prints 00:06:15.8