MDanz Posted July 8, 2010 Share Posted July 8, 2010 i'm trying to do you have to wait 30 seconds between posts. this isn't working though. when i test i post under 30 seconds between post and it keeps echo "worked";. what did i do wrong? mysql_connect("localhost", "Master", "pword"); mysql_select_db("db"); $visitor = $_SERVER['REMOTE_ADDR']; $lastpost = mysql_query("SELECT * FROM Stacks WHERE ip='$visitor' ORDER BY posted DESC")or die (mysql_error()); while($latest = mysql_fetch_assoc($lastpost)){ $thelatest= strtotime($latest['posted']); } echo (time() - $thelatest); if ((time() - $thelatest) > 30){ echo "worked"; } else { echo "you must wait 30 seconds"; } Quote Link to comment https://forums.phpfreaks.com/topic/207094-help-with-time-if-statement/ Share on other sites More sharing options...
Pikachu2000 Posted July 8, 2010 Share Posted July 8, 2010 How is the timestamp store in the database? Quote Link to comment https://forums.phpfreaks.com/topic/207094-help-with-time-if-statement/#findComment-1082843 Share on other sites More sharing options...
MDanz Posted July 8, 2010 Author Share Posted July 8, 2010 in the database the type is timestamp. I insert with NOW(). Quote Link to comment https://forums.phpfreaks.com/topic/207094-help-with-time-if-statement/#findComment-1082847 Share on other sites More sharing options...
DavidAM Posted July 8, 2010 Share Posted July 8, 2010 $lastpost = mysql_query("SELECT * FROM Stacks WHERE ip='$visitor' ORDER BY posted DESC")or die (mysql_error()); while($latest = mysql_fetch_assoc($lastpost)){ $thelatest= strtotime($latest['posted']); } That query is retrieving EVERY post from that user. The ORDER BY posted DESC means you get the newest one first and the oldest one last. Then you loop through the entire resultset calculating $thelatest. This means at the end of the while loop, $thelatest contains the date of the user's FIRST post. // 1) ADD A LIMIT TO THE QUERY $lastpost = mysql_query("SELECT * FROM Stacks WHERE ip='$visitor' ORDER BY posted DESC LIMIT 1")or die (mysql_error()); // 2) GET RID OF THE WHILE LOOP, YOU DO NOT NEED TO WALK THROUGH THE RESULTS $latest = mysql_fetch_assoc($lastpost)); $thelatest= strtotime($latest['posted']); Quote Link to comment https://forums.phpfreaks.com/topic/207094-help-with-time-if-statement/#findComment-1082851 Share on other sites More sharing options...
MDanz Posted July 8, 2010 Author Share Posted July 8, 2010 thanks...stupid me lol Quote Link to comment https://forums.phpfreaks.com/topic/207094-help-with-time-if-statement/#findComment-1082853 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.