将秒转换为HH:MM:SS [英] Converting Seconds to HH:MM:SS

查看:243
本文介绍了将秒转换为HH:MM:SS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数sec2hms(())()函数将秒转换为HH:MM:SS在线,函数是这样的: $ sec,$ padHours = false){
$ hms =;
$ hours = intval(intval($ sec)/ 3600);
$ hms。=($ padHours)
? str_pad($ hours,2,0,STR_PAD_LEFT)。 ':'
:$小时。 :;
$分钟= intval(($ sec / 60)%60);
$ hms。= str_pad($分钟,2,0,STR_PAD_LEFT)。 :;
$ seconds = intval($ sec%60);
$ hms。= str_pad($秒,2,0,STR_PAD_LEFT);
返回$ hms;
}

我知道这很好,就好像我做了 echo (sec2hms(3600)); 它会返回为 1:00:00



我想要发生的事情是使用该函数转换列 timeran 中的每个条目,然后将其显示在HTML表中,其中列用户名分数,我不确定如何做到这一点,所以如果有人可以帮助我,那就太棒了。我目前使用下面的代码来显示表格,但只显示了几秒钟内的时间,我希望它在HH:MM:SS:

 < TR> 
< th> Rank< / th>
< th>使用者名称< / th>
获得的时间(s)< / th>
分数< / th>
< / tr>
< / thead>
< tbody>
<?php
$ result = mysql_query(SELECT * FROM gamescores ORDER BY Time DESC); ($ rank = 1; $ row = mysql_fetch_assoc($ result); $ rank ++){
echo< tr> $ b $
if(mysql_num_rows($ result)){
b< td> {$ rank}< / td>
< td> {$ row ['username']}< / td>
< td> {$ row ['timetaken ']}< / td>
< td> {$ row ['score']}< / td>

如果有更好的方法将秒转换为HH:MM:SS,那么这也会非常棒,谢谢!

解决方案

使用MySQL SEC_TO_TIME函数。



示例:

  mysql> SELECT SEC_TO_TIME(2378); 
- > '00:39:38'

所以你可以把你的SQL查询写成:

  SELECT username,score,SEC_TO_TIME(timeran)as timetaken FROM gamescores ORDER BY Time DESC 

然后您的代码变为:

 <?php 
$ result = mysql_query(SELECT username,scor e,SEC_TO_TIME(timeran)作为timetaken FROM gamecores ORDER BY Time DESC); ($ rank = 1; $ row = mysql_fetch_assoc($ result); $ rank ++){
echo< tr> $ b $
if(mysql_num_rows($ result)){
b< td> {$ rank}< / td>
< td> {$ row ['username']}< / td>
< td> {$ row ['timetaken ']}< / td>
< td> {$ row ['score']}< / td>


I found a function to convert seconds to HH:MM:SS online, the function is this:

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;
}

I know this works fine as if I do echo(sec2hms(3600)); it will return as 1:00:00

What I want to happen is to use that function to convert every single entry in the column timeran and then display it in a HTML table with the columns username and score, I am not sure how to do this so if someone could help me out then that'd be great. I am currently using the code below to display the table but that only shows time taken in seconds and I would like it to be in 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>

If there is a better way of converting seconds to HH:MM:SS then that would be great too, thank you!

解决方案

Use the MySQL SEC_TO_TIME function.

Example:

mysql> SELECT SEC_TO_TIME(2378);
        -> '00:39:38'

So you can write your SQL query as:

SELECT username, score, SEC_TO_TIME(timeran) as timetaken FROM gamescores ORDER BY Time DESC

Your code then becomes:

<?php
$result = mysql_query("SELECT username, score, SEC_TO_TIME(timeran) as timetaken 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆