des*_*oid 14 php date time-format
我总共有几毫秒(即70370)并且我希望将其显示为分钟:秒:毫秒,即00:00:0000.
我怎么能用PHP做到这一点?
nic*_*ckf 34
不要陷入使用日期功能的陷阱!你在这里有一个时间间隔,而不是一个日期.天真的方法是做这样的事情:
date("H:i:s.u", $milliseconds / 1000)
Run Code Online (Sandbox Code Playgroud)
但由于日期函数用于(喘气!)日期,因此在格式化日期/时间时,它不会按照您希望的方式处理时间 - 它需要考虑时区和夏令时等.
相反,你可能只想做一些简单的数学:
$input = 70135;
$uSec = $input % 1000;
$input = floor($input / 1000);
$seconds = $input % 60;
$input = floor($input / 60);
$minutes = $input % 60;
$input = floor($input / 60);
// and so on, for as long as you require.
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 PHP 5.3,则可以使用该DateInterval对象:
list($seconds, $millis) = explode('.', $milliseconds / 1000);
$range = new DateInterval("PT{$seconds}S");
echo $range->format('%H:%I:%S') . ':' . str_pad($millis, 3, '0', STR_PAD_LEFT);
Run Code Online (Sandbox Code Playgroud)