我想将一定的秒数转换为几分钟甚至几小时。但是我不想为每分钟和每小时等写一个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;
答案 0 :(得分:4)
查看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
答案 1 :(得分:0)
使用mod
function convert_to_string_time($num_seconds)
{
$seconds = $num_seconds % 60;
$min = floor( $num_second / 60 );
if( $min == 0 )
return "{$seconds} sec";
else
return "{$min} min + {$seconds} sec";
}