0

I'm struggling to get my timestamp which has been converted to time ago to be displayed within a table. I've seen tutorial on how to have this displayed but haven't came across somebody trying to get it into a table.

<?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
?>

Edit:

I'm basically looking to have the Interval be displayed in my table, for each row that is displayed, in the

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

I hope this helps.

(very new to php sorry if you find this hard to understand as I can't explain well)

Edit:

Have given my own answer below, thank you all for any help given.

serenesat
  • 4,611
  • 10
  • 37
  • 53
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 13 '15 at 18:09
  • What is your question? – John Conde Jul 13 '15 at 18:09
  • what you actually want? where problem occur? what is input? what is expected output? what is currently you are getting in output? shows us. then we will assist you what's the problem? – Alive to die - Anant Jul 13 '15 at 18:12
  • I'm looking to have the interval displayed in the td that's currently echo "".$tblLobbies["time"].""; – Harrison Smith Jul 13 '15 at 18:26
  • @JayBlanchard thanks for the tip, will work on this – Harrison Smith Jul 13 '15 at 18:45

2 Answers2

0

In php, you have to instantiate a variable before you can use the variable. What you want is more like this:

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

Can you show us a SQL statement for the table you are working on?

VikingBlooded
  • 884
  • 1
  • 6
  • 17
  • Ok thanks for the answer, I'm trying to have the time display to be pulled from this: .$tblLobbies["time"]. – Harrison Smith Jul 13 '15 at 20:14
  • Again, you are calling the $time variable before you declare what it is.... move the $dbDate = new DateTime($time) to AFTER $time = strtotime($tblLobbies['time']); – VikingBlooded Jul 13 '15 at 20:18
  • 'Fatal error: Uncaught exception 'Exception' with message `DateTime::__construct(): Failed to parse time string (1436792919) at position 8 (1): Unexpected character' in /home/csgolobbies/public_html/index.php:183 Stack trace: #0 /home/csgolobbies/public_html/index.php(183): DateTime->__construct('1436792919') #1 {main} thrown in` This is the error that doing so gives – Harrison Smith Jul 13 '15 at 20:25
  • gah, sorry, move it back and remove the $time from $dbDate = new DateTime($time) – VikingBlooded Jul 13 '15 at 20:26
  • Thanks have seen your editted post now and have applied this to my code, here is how the code currently looks and the error being given: http://prntscr.com/7sb5no http://prntscr.com/7sb6bo – Harrison Smith Jul 13 '15 at 20:46
  • I've only got 1 rep, is it possible you can move this discussion to chat? – Harrison Smith Jul 13 '15 at 20:50
  • Sorry mate, you can't chat at all with only 1 rep. Hate to do this, but you should also take a look at http://php.net/manual/en/datetime.diff.php – VikingBlooded Jul 13 '15 at 21:01
  • Haven't got it working yet, but I'm going to give you an upvote for all the help you have given, thanks – Harrison Smith Jul 13 '15 at 21:16
  • can you show us a SQL statement for the table you are working with? – VikingBlooded Jul 13 '15 at 21:17
  • Have just seen your reply $sql="SELECT * FROM tblLobbies ORDER BY time DESC"; $records=mysql_query($sql); – Harrison Smith Jul 13 '15 at 21:30
  • Here's what was given: resource(5) of type (mysql result) – Harrison Smith Jul 13 '15 at 21:39
  • Can you show us an example of the SQL that builds the table itself? I get the feeling that you are calling a column that isnt there. – VikingBlooded Jul 13 '15 at 21:41
  • I think I see an issue. Your time field is set to type "time" which in mysql, is just a time format of HH:MM:SS. You're trying to put a DateTime into that field which is a big no-no. So there's one part of it. Working on the rest for you later. – VikingBlooded Jul 14 '15 at 16:14
  • thanks for all the help, I've managed to get another solution working however. Much appreciated for your time on this – Harrison Smith Jul 14 '15 at 18:04
0

Thank you to everyone who has helped out with this problem. My friend spent the day trying to fix this and here's the solution that is currently working on our site:

<?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

?>

Big thank you to @VikingBlooded who spent a while going through possible problems and how to solve them yesterday.