Terminaxx Posted June 17, 2017 Share Posted June 17, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/304159-how-to-subtract-current-time-with-database-time/ Share on other sites More sharing options...
Solution Jacques1 Posted June 17, 2017 Solution Share Posted June 17, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/304159-how-to-subtract-current-time-with-database-time/#findComment-1547479 Share on other sites More sharing options...
Terminaxx Posted June 17, 2017 Author Share Posted June 17, 2017 Thank you once again. I forgot that it can be that easily done. Quote Link to comment https://forums.phpfreaks.com/topic/304159-how-to-subtract-current-time-with-database-time/#findComment-1547480 Share on other sites More sharing options...
Terminaxx Posted June 17, 2017 Author Share Posted June 17, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/304159-how-to-subtract-current-time-with-database-time/#findComment-1547481 Share on other sites More sharing options...
Jacques1 Posted June 17, 2017 Share Posted June 17, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/304159-how-to-subtract-current-time-with-database-time/#findComment-1547482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.