PHP:将秒转换为分钟和小时 [英] PHP: convert seconds to minutes and hours

查看:851
本文介绍了PHP:将秒转换为分钟和小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一定数量的秒转换为几分钟甚至几小时.但是我不想为每一分钟和每一小时写一个if子句.

I would like to convert a certain number of seconds to minutes and even hours. But i don't want to write an if clause for every minute and hour etc. ...

我该如何以最简单的方式做到这一点,而最简单的方式是最短的意思. ;)

How can I do that in the easiest way, and with easiest I mean shortest. ;)

PHP:

$countholen = mysqli_fetch_array(mysqli_query($db, "
SELECT * FROM `blablabla` WHERE `blabla` = 'bla'
"));
$countholenfetch = $countholen["count"];
if ($countholenfetch <= 60){
    $count = $countholenfetch . " sec";
}
if ($countholenfetch > 60){
    $countholenfetch = $countholenfetch - 60;
    $count = "1 min" . " + " . $countholenfetch . " sec";
}
//...if clause with 120, 180, 240 etc. instead of 60 till 3600 and another if clause in an if clause...
echo $count;

推荐答案

看看 gmdate() 功能.

Take a look at the gmdate() function.

$countholen = mysqli_fetch_array(mysqli_query($db, "
SELECT * FROM `blablabla` WHERE `blabla` = 'bla'
"));
$countholenfetch = $countholen["count"];

echo gmdate("H:i:s", $countholenfetch);

注意:如果要处理大量数字,请改用类似的方法

Note: If you are working with large numbers, then use something like this instead,

$seconds = 86401 ;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;

echo "$hours:$minutes:$seconds"; //24:0:1

这篇关于PHP:将秒转换为分钟和小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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