Link to home
Start Free TrialLog in
Avatar of classic_gaming
classic_gaming

asked on

Comparing mysql now() timestamp with php

hi,

i'm trying to check if the user has submitted less than 10mins ago in the database but the code i got, dont seem to work. (the code is grabbed from the php file so start/end stuff might not be there.)

$squ = "select subip, dte from dbwr WHERE subip='$usip' ORDER BY dte ASC LIMIT 1";
$res = mysql_query($squ) or die(mysql_error());
while ($newarray = mysql_fetch_array($res)) {
$mytime = $newarray['dte'];

//this is where it checks if the time in the db is less than 10mins
if ($mytime >= time()-600) $abuse = 1;
else
$abuse = 0;

the query i submit is to grab the ip and timestamp from the db and return the newest one. (dte = timestamp, subip = userip)

hope you can help me asap.

cheers
Avatar of lozloz
lozloz

hi,

your code is missing a }, or you could see it as an extra while clause (i've taken it out)

<?php
$squ = "select subip, dte from dbwr WHERE subip='$usip' ORDER BY dte ASC LIMIT 1";
$res = mysql_query($squ) or die(mysql_error());
$newarray = mysql_fetch_assoc($res);
$mytime = $newarray['dte'];
//this is where it checks if the time in the db is less than 10mins
if ($mytime >= (time()-600)) $abuse = 1;
else
$abuse = 0;
?>
Avatar of classic_gaming

ASKER

hi,

but does that time compare if statement work?

that is the prime thing that needs sorting.

for example:

dte = 20031203133421
and if time() = 20031203134321

that should equal 9mins.

i dont have a clue if the if statement works or not, that is why im asking :(

cheers
Avatar of Giovanni G
Yes it should work.. isnt it?
Please provide some test cases.. like

print "$mytime -- " . (time() - 600) . " -- $abuse";

i hope you are inserting as php's time() in the database, because if you are doing it with MySQL function UNIX_TIMESTAMP() the clock difference might be fatal.
that time stamp that you've provided for dte isn't a php timestamp, so you need to decide whether you want to use a php timestamp or mysql one.. personally i use php

loz
<?php
//this is what i do to submit the data
$newt = time();
mysql_query("INSERT INTO dbwr VALUES ('$usip'," . $newt . ",'0')");

//this is what i do to get the time from the db
$squ = "select subip, dte from dbwr WHERE subip='$usip' ORDER BY dte ASC LIMIT 1";
$res = mysql_query($squ) or die(mysql_error());
$newarray = mysql_fetch_assoc($res);
$mytime = $newarray['dte'];
//this is where it checks if the time in the db is less than 10mins
if ($mytime >= (time()-600)) $abuse = 1;
else
$abuse = 0;
?>
is there anything wrong im doing?
i assume $newt is being inserted for the column dte - this is a php timestamp and so correct, the code should work. if it isn't, do the debugging ThG suggested

loz
dte = 20001210021847

print "$mytime -- " . (time() - 600) . " -- $abuse"; = 20001210021847 -- 1070501259 -- 1

does that help enlighten anything?
i think you've got the wrong type of mysql column, what is it? it should be int(11)

loz
ah must be why, it was timestamp(15), changed it to int(11)

i've also changed the select query to DESC

dte = 1070503722

new submission:
Abuse!
1070503722 -- 1070503221 -- 1
-699

echo "Abuse!<br>";
$nt = $mytime - time() - 600;
print "$mytime -- " . (time() - 600) . " -- $abuse<br>$nt";

why is it going past 600 and denying me to submit new data?
ASKER CERTIFIED SOLUTION
Avatar of lozloz
lozloz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ah it works now, thanks for your help
A faster a possibly simpler solution depending on what else you have to do....
<?PHP

$10mins_ago = time() - 600;

$squ = "select `subip`,`dte` from `dbwr` WHERE `subip`='$usip' AND `dte`>=$10mins_ago";
$res = mysql_query($squ) or die(mysql_error());
$num_returns = mysql_num_rows($res);

if($num_returns==0){
      //abuse
}
else{
      //no abuse
}

?>