Jump to content

How to subtract current time with database time?


Terminaxx

Recommended Posts

Hey Guys.

 

I tried this:

while($row = mysqli_fetch_object($res)) {
    if (round((time() - $row->date) / 60,2) < 0){


         echo "<tr class='tablerow'>";
         echo "<td class='tabledata'>".$row->type."</td>";

    }
}


I cut a little bit off from the table.

 

But the point is, I am trying to show the tabledata only if the current time isn't older than the time written in the Database.

 

The Database date is written like this 2017-06-17 20:00:00 and a timestamp.

 

Thanks for any advice

Link to comment
Share on other sites

So do you want future records or past records? Your code and your description say two different things.

 

In any case, filtering is done with the WHERE clause, not in the application. And, yes, MySQL understands the concept of time.

...
WHERE
    date >= NOW()    -- only current and future records; adjust if necessary
Link to comment
Share on other sites

 

So do you want future records or past records? Your code and your description say two different things.

 

In any case, filtering is done with the WHERE clause, not in the application. And, yes, MySQL understands the concept of time.

...
WHERE
    date >= NOW()    -- only current and future records; adjust if necessary

 

 

But I got one last question.

With the tabledata you helped me create, i also create small submit buttons for 3 options. Obviously when someone doesnt reload the site and the time is now over, the data will still stand there until he reloads it. Therefore, even if the time is over, he still can submit one of the 3 buttons.

$res = mysqli_fetch_object(mysqli_query($con, "SELECT * FROM games WHERE id = $game"));

if (!empty($res) and round((time() - $res->date) / 60,2) < 0){



therefore i tried this again. This time its not possible to do it like above or am i missing something?

Link to comment
Share on other sites

Your time comparison code makes no sense, so you should throw it away entirely.

 

Simply repeat the WHERE clause when you do your UPDATE query (or whatever it is you want to do).

UPDATE ...
SET
  ...
WHERE
  date >= NOW()  -- prevent the user from updating old records

The affected rows tell you whether or not the row has in fact been updated. If it hasn't, you inform the user that the record has already expired -- assuming that's the only possible reason.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.