Jump to content

How to subtract current time with database time?


Go to solution Solved by Jacques1,

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

  • Solution

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

 

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.