如何在 PHP 中将毫秒数格式化为分钟:秒:毫秒?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1833869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:59:47  来源:igfitidea点击:

How do I format an amount of milliseconds into minutes:seconds:milliseconds in PHP?

phpdatetime-format

提问by designvoid

I have a total ammount of milliseconds (ie 70370) and I want to display it as minutes:seconds:milliseconds ie 00:00:0000.

我有总毫秒数(即 70370),我想将其显示为分钟:秒:毫秒,即 00:00:0000。

How can I do this in PHP?

我怎样才能在 PHP 中做到这一点?

回答by nickf

Don't fall into the trap of using date functions for this! What you have here is a time interval, not a date. The naive approach is to do something like this:

不要陷入为此使用日期函数的陷阱!您在这里拥有的是时间间隔,而不是日期。天真的方法是做这样的事情:

date("H:i:s.u", $milliseconds / 1000)

but because the date function is used for (gasp!) dates, it doesn't handle time the way you would want it to in this situation - it takes timezones and daylight savings, etc, into account when formatting a date/time.

但是因为日期函数用于(喘气!)日期,它在这种情况下不会按照您希望的方式处理时间 - 在格式化日期/时间时需要考虑时区和夏令时等。

Instead, you will probably just want to do some simple maths:

相反,您可能只想做一些简单的数学运算:

$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.

回答by soulmerge

If you are using PHP 5.3 you can make use of the DateIntervalobject:

如果您使用的是 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);

回答by jab11

why bother with date()and formatting when you can just use math ? if $msis your number of milliseconds

date()当您可以只使用数学时,为什么还要麻烦和格式化?如果$ms是你的毫秒数

echo floor($ms/60000).':'.floor(($ms%60000)/1000).':'.str_pad(floor($ms%1000),3,'0', STR_PAD_LEFT);

回答by Kamil Szot

Try this function to display amount of milliseconds the way you like:

试试这个函数以你喜欢的方式显示毫秒数:

<?php
function udate($format, $utimestamp = null)
{
   if (is_null($utimestamp)) {
       $utimestamp = microtime(true);
   }

   $timestamp = floor($utimestamp);
   $milliseconds = round(($utimestamp - $timestamp) * 1000000);

   return date(preg_replace('`(?<!\\)u`', sprintf("%06u", $milliseconds), $format), $timestamp);
}

echo udate('H:i:s.u'); // 19:40:56.78128
echo udate('H:i:s.u', 654532123.04546); // 16:28:43.045460
?>

Source

来源

回答by Alan Storm

I believe there's no built-in function for formatting miliseconds in PHP, you'll need to use maths.

我相信 PHP 中没有用于格式化毫秒的内置函数,您需要使用数学。

回答by Julius F

As mentioned in the manual:

正如手册中提到的:

u Microseconds (added in PHP 5.2.2) Example: 654321

u 微秒(PHP 5.2.2 中新增) 示例:654321

We have a 'u' parameter for the date() function

我们有一个用于 date() 函数的 'u' 参数

Example:

例子:

if(($u/60) >= 60)
{
$u = mktime(0,($u / 360));
}
date('H:i:s',$u);

回答by Azouz Mohamadi

convert milliseconds to formatted time

将毫秒转换为格式化时间

<?php
/* Write your PHP code here */
$input = 7013512333;

$uSec = $input % 1000;
$input = floor($input / 1000);

$seconds = $input % 60;
$input = floor($input / 60);

$minutes = $input % 60;
$input = floor($input / 60);

$hour = $input ;

echo sprintf('%02d %02d %02d %03d', $hour, $minutes, $seconds, $uSec);
?>

check demo here : https://www.phprun.org/qCbY2n

在此处查看演示:https: //www.phprun.org/qCbY2n