Php 将int-echo毫秒转换为小步和秒
我有一个Php 将int-echo毫秒转换为小步和秒,php,mysql,Php,Mysql,我有一个php查询,它从我的数据库中获取一个包含int的字段,例如7800。我知道7800是7.7秒或0.13分钟 我需要一种方式将其显示为00:00:08(向上舍入) 表格打印输出如下所示: <td><?= $row["timeSpent"] ?></td> 除了7.8seconds打印为02:10:00外,您只需使用构建变量,并用前导零填充字符串位: <?php function convertToTime($time) { $time
php
查询,它从我的数据库中获取一个包含int的字段,例如7800。我知道7800是7.7秒或0.13分钟
我需要一种方式将其显示为00:00:08
(向上舍入)
表格打印输出如下所示:
<td><?= $row["timeSpent"] ?></td>
除了7.8
seconds打印为02:10:00
外,您只需使用构建变量,并用前导零填充字符串位:
<?php
function convertToTime($time) {
$time = round($time/1000);
$hours = floor($time/3600);
$time = $time%3600;
$minutes = floor($time/60);
$seconds = $time%60;
return str_pad($hours, 2, '0',STR_PAD_LEFT) . ':' . str_pad($minutes, 2, '0',STR_PAD_LEFT) . ':' . str_pad($seconds, 2, '0',STR_PAD_LEFT);
}
var_dump(convertToTime(7800)); // string '00:00:08' (length=8)
var_dump(convertToTime(60000)); // string '00:01:00' (length=8)
var_dump(convertToTime(3600000)); // string '01:00:00' (length=8)
var_dump(convertToTime(42687000)); // string '11:51:27' (length=8)
?>
使用round()函数easy enouth,是的。但是我问的是如何将int-echo转换为(00:00:08),而不是如何将7.8到8毫米的值四舍五入,答案不错。这看起来很有希望。我应该在哪里将我的表行“timespunt”放入此中?您必须声明convertToTime
函数,然后在$row
变量上使用它,类似于
谢谢,我将完成此工作,然后接受您回答+1的解释。因此,请尽量避免使用PHP的简短开头标记Top-tip,我不知道+1
<?php
function convertToTime($time) {
$time = round($time/1000);
$hours = floor($time/3600);
$time = $time%3600;
$minutes = floor($time/60);
$seconds = $time%60;
return str_pad($hours, 2, '0',STR_PAD_LEFT) . ':' . str_pad($minutes, 2, '0',STR_PAD_LEFT) . ':' . str_pad($seconds, 2, '0',STR_PAD_LEFT);
}
var_dump(convertToTime(7800)); // string '00:00:08' (length=8)
var_dump(convertToTime(60000)); // string '00:01:00' (length=8)
var_dump(convertToTime(3600000)); // string '01:00:00' (length=8)
var_dump(convertToTime(42687000)); // string '11:51:27' (length=8)
?>