我在网上找到了一个将秒转换为HH:MM:SS的函数,函数是这样的:
function sec2hms ($sec, $padHours = false) {
$hms = "";
$hours = intval(intval($sec) / 3600);
$hms .= ($padHours)
? str_pad($hours, 2, "0", STR_PAD_LEFT). ':'
: $hours. ':';
$minutes = intval(($sec / 60) % 60);
$hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
$seconds = intval($sec % 60);
$hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
return $hms;
}
我知道这工作得很好,就像我做的一样echo(sec2hms(3600));
它将返回为1:00:00
我想要发生的是使用该函数来转换列中的每个条目timeran
然后将其显示在包含列的 HTML 表格中username
and score
,我不知道如何做到这一点,所以如果有人可以帮助我那就太好了。我目前正在使用下面的代码来显示表格,但只显示以秒为单位的时间,我希望它以 HH:MM:SS 为单位:
<tr>
<th>Rank</th>
<th>Username</th>
<th>Time Taken (s)</th>
<th>Score</th>
</tr>
</thead>
<tbody>
<?php
$result = mysql_query("SELECT * FROM gamescores ORDER BY Time DESC");
if (mysql_num_rows($result)) {
for($rank=1; $row = mysql_fetch_assoc($result); $rank++) {
echo "<tr>
<td>{$rank}</td>
<td>{$row['username']}</td>
<td>{$row['timetaken']}</td>
<td>{$row['score']}</td>
如果有更好的方法将秒转换为 HH:MM:SS 那么那就太好了。