我想把一定的秒数转换成分钟甚至小时。但我不想每分钟每小时都写一个if从句…
我怎么能用最简单的方法做到这一点,用最简单的方法就是最短的。;
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()
函数。
$countholen = mysqli_fetch_array(mysqli_query($db, "
SELECT * FROM `blablabla` WHERE `blabla` = 'bla'
"));
$countholenfetch = $countholen["count"];
echo gmdate("H:i:s", $countholenfetch);
注意:如果你用的是大数字,那么就用这样的方法来代替,
$seconds = 86401 ;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
echo "$hours:$minutes:$seconds"; //24:0:1