如何格式化数毫秒到几分钟:秒:毫秒在PHP? [英] How do I format an amount of milliseconds into minutes:seconds:milliseconds in PHP?

查看:863
本文介绍了如何格式化数毫秒到几分钟:秒:毫秒在PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我在PHP中这样做?

推荐答案

不要陷入使用日期功能的陷阱!你在这里是一个时间间隔,而不是一个日期。天真的做法是这样做:

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", $mytime / 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.

这篇关于如何格式化数毫秒到几分钟:秒:毫秒在PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆