我有一个从XML抓取到foreach
中的游戏和小时数的列表。他们将时间格式化为 1.8 小时而不是 1 小时 48 分钟。
以下代码中的所有内容都正常工作,除了最后一个elseif
。它应该抓取超过 6 分钟但少于 60 分钟的游戏,对其进行转换,然后在末尾添加"分钟"。
目前,它只显示所有这些游戏的"0分钟"。
$hoursplayed = $game->hoursOnRecord;
if ($hoursplayed < .1) {
$zeros = $zeros + 1;
$hoursplayed = 0;}
elseif ($hoursplayed >= 1){
$nonzeros = $nonzeros + 1;
}
elseif ($hoursplayed >= .1 && $hoursplayed < 1) {
$hoursplayed = ($hoursplayed * 60) . " minutes";
}
抱歉,它运行良好!
在这里工作演示。
试试这个:
$hoursplayed = (float) $game->hoursOnRecord;
你的代码很乱;这就是我写它的方式:
<?php
$hours_float = $game -> hoursOnRecord;
$hours = floor($hours_float);
$minutes = round(($hours_float - $hours) * 60);
if (($hours > 0 || $minutes > 6) && $hours < 1)
{
echo $hours . ' hour' . ($hours === 1 ? '' : 's') . ' ' . $minutes . ' minute' . ($minutes === 1 ? '' : 's');
}