So I am trying to setup an online list for my website and I seem to be having a problem. I have done this before, but I have not php'd in about a year and need a little refreshing time to time.

At this point I am stuck trying to edit a time stamp to show that if a user has been online in the last ten minutes then show them online.

So this is what my script looks like. I'll put some comments into it and if there is anything you can think of that will help then please do so. Thanks.

<?php
include 'connect.php';
include 'header.php';

if(isset($_COOKIE['member_id'])){
 $Time=date("H:i:s");
 $Player=$_COOKIE['member_id'];
 mysql_query("Update Users set Online='$Time' where ID='$Player'");
}
echo"<h1><u>Online</u></h1>";
echo"<table class=\"table\"><tr><td>Username</td><td>Level</td></tr>";

  $TimeFrame=date("H:i:s");//using server time
  $TimeFrame=$TimeFrame-600;//subtracting 600 seconds, aka 10 minutes
  print $TimeFrame;//prints out a negative 3 digit number like \"-592\"

  $On="SELECT * from Users where Online>='$TimeFrame'";
  $On2=mysql_query($On) or die("Could not query players");
  while($Online=mysql_fetch_array($On2))//shows all players online, even inactive
  {
    echo"<tr><td>$Online[Playername]</td><td>$Online[Level]</td></tr>";
  }

echo"</table>";

?>

So anything commented on in this script is where there is a potential problem.

    [man]date[/man] returns a string. you should look it up in the documentation.

    not sure how your db is structured, but you should probably be subtracting 600 seconds from the output of [man]time[/man] instead, and then maybe convert that to something your can compare to your db.

      Still not sure what you mean. Anyway you can explain a little more. As far as I see your trying to tell me that my $TimeFrame variable is not the output?

      To me it looks like it is the output. Showing someone that the server time is $TimeFrame. And I print it to see where I can find and error but it's subtracting a little different than I would want.

      Without the "-600" I get a normal time response in the print, like "12:46:12". With the "-600" I receive back a number in the negatives printing time like "-588".

      And like I said before it starts displaying all players rather than the ones on in the last ten minutes.

      Sorry, don't mean to confuse or correct you. Only wish to clairify a little more in hope that you understand where I'm trying to come from. Any help is great! Thanks for the response.

        On table creation did u set field type to timestamp or DATETIME ect..;, if U have a default 4 your field eg. 2010-07-01 you must first convert your timestamp to an acceptable number using, be sure u are working
        with the same data types, eg.
        time() returns a timestamp,
        mktime returns a timestamp,
        date() can be configured to return a date from a timestamp ect.., try catch, find the error.

          well before the data base stuff I want to figure out why print $TimeFrame isn't working.

            Like I said, [man]date[/man] returns a [man]string[/man]. To try and subtract an integer value of 600 from a string makes no sense in a computer program. It's like telling a 4th grader to multiply the moon by a cow. Strings and integers: Apples and oranges. If you had read the documentation which I so kindly linked, you'd know a bit more about how it works. Read the examples and stuff and you'll probably find something similar to what you are after.

            the [man]time[/man] function returns the current time as an integer value. The result being an integer, you can subtract from it nicely. Now the question is do you want to subtract 600 or some other value? Since time returns a value in seconds, 600 seconds (10 minutes) might be a reasonable amount to subtract.

              I don't even see a need for $Time or $TimeFrame at all. Note that the rest of this post assumes that 'Online' is a DATETIME (or TIMESTAMP) column, since that's what it sounds like it should be.

              For the first query, why not just do this:

              "Update Users set Online=NOW() where ID='$Player'"

              For the second query, why not just do this:

              "SELECT * from Users where Online >= NOW() - INTERVAL 10 MINUTE"
                Write a Reply...