我从数据库中获取了一系列代表竞争对手单圈时间的次数。我将数据库设置为TIME,显示00:00:00。它最后也加总了总数。
我想要更改它,以便它不显示所有的0,如果例如输入53秒的时间(0:53)将被显示。我想这样做只是为了整理网页。对于上面示例中显示00:00:53的总数,我希望它为0:53。如果有一分钟显示1:53,十分钟10:53,小时1:10:53等等。
<?php
//MySqli Select Query
$results = $mysqli->query("SELECT *, TIME(r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9 + r10 + r11 + r12 + r13 + r14 + r15 + r16 + r17 + r18 + r19 + r20 + r21 + r22 + r23 + r24 + r25 + r26 + r27 + r28 + r29 + r30 + r31 + r32 + r33 + r34 + r35 + r36 + r37 + r38 + r39 + r40 + r41 + r42 + r43 + r44 + r45 + r46 + r47 + r48 + r49 + r50) AS total FROM test");
$title = $mysqli->query("SELECT * FROM test_info");
$all_cols=array("rt1" => "r1","rt2"=> "r2","rt3"=> "r3","rt4"=> "r4","rt5"=> "r5","rt6" => "r6","rt7"=> "r7","rt8"=> "r8","rt9"=> "r9","rt10"=> "r10","rt11" => "r11","rt12"=> "r12","rt13"=> "r13","rt14"=> "r14","rt15"=> "r15","rt16" => "r16","rt17"=> "r17","rt18"=> "r18","rt19"=> "r19","rt20"=> "r20","rt21" => "r21","rt22"=> "r22","rt23"=> "r23","rt24"=> "r24","rt25"=> "r25","rt26" => "r26","rt27"=> "r27","rt28"=> "r28","rt29"=> "r29","rt30"=> "r30","rt31" => "r31","rt32"=> "r32","rt33"=> "r33","rt34"=> "r34","rt35"=> "r35","rt36" => "r36","rt37"=> "r37","rt38"=> "r38","rt39"=> "r39","rt40"=> "r40","rt41" => "r41","rt42"=> "r42","rt43"=> "r43","rt44"=> "r44","rt45"=> "r45","rt46" => "r46","rt47"=> "r47","rt48"=> "r48","rt49"=> "r49","rt50"=> "r50",);
while ($t = $title->fetch_assoc()){
// remember empty cols
$empty_cols=array();
echo "<table width=\"100%\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>";
foreach ($all_cols as $col => $value) {
if (!empty($t[$col])) {
echo "<td>" . $t[$col] . "</td>";
} else {
// set this column as empty for later
$empty_cols[]=$col;
}
} unset($col); unset($value);
echo "
<td>Total</td>
</tr>";
}
//set counter
$counter = 1;
while($row = $results->fetch_assoc()) {
echo "<tr align=\"center\">";
echo "<td>" . $counter . "</td>";
echo '<td>'.$row["driver"].'</td>';
foreach ($all_cols as $col => $value) {
if (!in_array($col, $empty_cols)) {
// echo non-empty values
echo '<td>'.$row[$value].'</td>';
}
} unset($col); unset($value);
echo '<td>'.$row["total"].'</td>';
$counter++; //increment count by 1
}
echo "</table>";
?>
所以我需要r1 - r50才能显示简短而非完整的00:00:00输入的时间,并且总计也是如此。
所以我在test_info
中添加了select *function timeformat($time) {
// Convenience identifiers
$hh = 0; $mm = 0; $ss = 0;
// Explode the time
$timecomp = explode($time);
// Analyse and return
if ($timecomp[$hh] = "00" && $timecomp[$mm] = "00") {
return '0:'.$ss;
} else
// Continue remaining tests from hereon
if ($timecomp[$hh] = "00" && $timecomp[$mm] = "01") {
return ':'.$mm.$ss;
}
}
我已将代码中的两行替换为
echo '<td>'.timeformat($row[$value]).'</td>';
echo '<td>'.timeformat($row["total"]).'</td>';
我正在
explode()需要至少2个参数,1在
中给出答案 0 :(得分:2)
您可以编写一个以hh:mm:ss格式占用时间的函数,并以您想要的格式返回一个字符串。
功能代码:
function formattime($time) {
$timecomp = explode(':', $time);
$hh = (int) $timecomp[0]; // Cast as integer
$mm = (int) $timecomp[1]; // Cast as integer
$ss = $timecomp[2]; // Keep '00' format
if ($hh == 0) {
return $mm.':'.$ss;
} else {
return $hh.':'.$mm.':'.$ss;
}
}
执行:
echo (formattime('00:00:53')); echo '<br>';
echo (formattime('02:00:53')); echo '<br>';
echo (formattime('00:07:53')); echo '<br>';
给出:
0:53
2:0:53
7点53