Jump to content

help with time, if statement


MDanz

Recommended Posts

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";

}

Link to comment
https://forums.phpfreaks.com/topic/207094-help-with-time-if-statement/
Share on other sites

$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']);

 

 

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.