使用 PHP 始终将时间显示为 XX:XX

发布于 2024-12-29 16:16:06 字数 281 浏览 6 评论 0原文

全部, 我有以下代码来根据提供的毫秒计算时间:

$ms = $value['trackTimeMillis'];
$track_time = floor($ms/60000).':'.floor(($ms%60000)/1000);

问题是有时这效果不太好。例如,如果我的毫秒数为 246995,则输出为 4:6。

有没有办法始终使其正确转换,并且如果它确实四舍五入为偶数,则在其末尾添加零?那么像 2:3 这样的内容会读作 2:30 吗?

谢谢!

All,
I have the following code to figure out a time based on milliseconds that were provided:

$ms = $value['trackTimeMillis'];
$track_time = floor($ms/60000).':'.floor(($ms%60000)/1000);

The issue is that sometimes this doesn't work that well. For example, if I have the milliseconds as 246995 this will output 4:6.

Is there a way to always make it so that it converts this correct and if it does round to an even number to add a zero at the end of it? So something like 2:3 would read 2:30?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2025-01-05 16:16:07

是的:

sprintf("%d:%02d", floor($ms / 60000), floor($ms % 60000) / 1000);

Yes:

sprintf("%d:%02d", floor($ms / 60000), floor($ms % 60000) / 1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文