PHP mysql显示时间表

时间:2015-07-13 18:08:06

标签: php mysql

我正在努力让我的时间戳已被转换为时间以显示在表格中。我已经看过如何显示这个教程的教程,但没有遇到有人想把它放到桌子里。

<?php

$time = strtotime($tblLobbies['time']);
$dbDate = new DateTime($time);
$currDate = new DateTime();
$interval = $currDate->diff($dbDate);
echo $interval->h." hours ".$interval->m." minutes ago";

while($tblLobbies=mysql_fetch_assoc($records)) {


echo "<tr>";
echo "<td><a href='".$tblLobbies["formLobbyID"]."'>Click here to join Lobby</a></td>";
echo "<td><a href='".$tblLobbies["formSteamID"]."'>".$tblLobbies["formSteamName"]."</td>";
echo "<td>".$tblLobbies["formYourRank"]."</td>";
echo "<td>".$tblLobbies["formRankGroup"]."</td>";
echo "<td>".$tblLobbies["formLobbyType"]."</td>";
echo "<td><img src='".$tblLobbies['formLocation']."' width=\"32\"></td>";
echo "<td>".$tblLobbies["time"]."</td>";
echo "</tr>";

} //end while
?>

修改

我基本上希望在我的表中显示Interval,对于显示的每一行,在

echo "<td>".$tblLobbies["time"]."</td>";

我希望这会有所帮助。

(非常新的PHP抱歉,如果你发现这很难理解,因为我无法解释清楚)

修改

下面给出了我自己的答案,谢谢大家给予的任何帮助。

2 个答案:

答案 0 :(得分:0)

在php中,您必须在使用变量之前实例化变量。你想要的更像是这样:

while($tblLobbies=mysql_fetch_assoc($records)) {
$time = strtotime($tblLobbies['time']);
$currDate = new DateTime();
$interval = $currDate->diff($time);

echo "<tr>";
echo "<td><a href='".$tblLobbies["formLobbyID"]."'>Click here to join Lobby</a></td>";
echo "<td><a href='".$tblLobbies["formSteamID"]."'>".$tblLobbies["formSteamName"]."</td>";
echo "<td>".$tblLobbies["formYourRank"]."</td>";
echo "<td>".$tblLobbies["formRankGroup"]."</td>";
echo "<td>".$tblLobbies["formLobbyType"]."</td>";
echo "<td><img src='".$tblLobbies["formLocation"]."' width=\"32\"></td>";
echo "<td>". $interval->format("%h")." hours ".$interval->("%m")." minutes ago"."</td>";
echo "</tr>";

} //end while

您能告诉我们您正在处理的表的SQL语句吗?

答案 1 :(得分:0)

感谢所有帮助解决此问题的人。我的朋友花了一天时间试图解决这个问题,这是目前正在我们网站上运行的解决方案:

<?php
//big int 20
//ctrl f timer

$get = mysql_query("SELECT * FROM tblLobbies ORDER BY time DESC");
while ($tblLobbies = mysql_fetch_assoc($get))
{
    //data
    $get_time = $tblLobbies['time'];
    $time = time();
    $diff = $time - $get_time;


    switch(1)
    {
        case ($diff < 60);
        $count = $diff;
        if ($count==0)
            $count = "moments";
        elseif ($count==1)
            $suffix = "second";
        else
            $suffix = "seconds";
        break;

        case ($diff > 60 && $diff < 3600);
        $count = floor($diff/60);
        if ($count==1)
            $suffix = "minute";
        else
            $suffix = "minutes";
        break;

        case ($diff > 3600 && $diff < 86400);
        $count = floor($diff/3600);
        if ($count==1)
            $suffix = "hour";
        else
            $suffix = "hours";
        break;

        case ($diff > 86400 && $diff < 2629743);
        $count = floor($diff/86400);
        if ($count==1)
            $suffix = "day";
        else
            $suffix = "days";
        break;

        case ($diff > 2629743 && $diff < 31556926);
        $count = floor($diff/2629743);
        if ($count==1)
            $suffix = "month";
        else
            $suffix = "months";
        break;

        case ($diff > 31556926);
        $count = floor($diff/31556926);
        if ($count==1)
            $suffix = "year";
        else
            $suffix = "years";
        break;

        }



echo "<tr>";
echo "<td><a href='".$tblLobbies["formLobbyID"]."'>Click here to join Lobby</a></td>";
echo "<td><a href='".$tblLobbies["formSteamID"]."'>".$tblLobbies["formSteamName"]."</td>";
echo "<td>".$tblLobbies["formYourRank"]."</td>";
echo "<td>".$tblLobbies["formRankGroup"]."</td>";
echo "<td>".$tblLobbies["formLobbyType"]."</td>";
echo "<td><img src='".$tblLobbies["formLocation"]."' width=\"32\"></td>";
echo "<td>".$count." ".$suffix." ago"."</td>";
echo "</tr>";

} //end while

?>

非常感谢@VikingBlooded,他花了一段时间解决可能出现的问题以及如何解决这些问题。