Solved

Comparing mysql now() timestamp with php

Posted on 2003-12-03
13
42,351 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:classic_gaming
13 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9870433
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;
?>
0
 

Author Comment

by:classic_gaming
ID: 9870576
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
0
 
LVL 14

Expert Comment

by:ThG
ID: 9870964
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.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 13

Expert Comment

by:lozloz
ID: 9871160
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
0
 

Author Comment

by:classic_gaming
ID: 9871302
<?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;
?>
0
 

Author Comment

by:classic_gaming
ID: 9871318
is there anything wrong im doing?
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9871438
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
0
 

Author Comment

by:classic_gaming
ID: 9871537
dte = 20001210021847

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

does that help enlighten anything?
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9871543
i think you've got the wrong type of mysql column, what is it? it should be int(11)

loz
0
 

Author Comment

by:classic_gaming
ID: 9871671
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?
0
 
LVL 13

Accepted Solution

by:
lozloz earned 200 total points
ID: 9871722
because you're doing it wrong - you're saying minus 600 from the old timestamp, then minus the time from that. so if $mytime is 5 minutes ago, you're making it 15 minutes ago compared to the timestamp, and then turning it in to a negative number in relation to 0

you want $nt = $mytime - (time() - 600);

loz
0
 

Author Comment

by:classic_gaming
ID: 9873729
ah it works now, thanks for your help
0
 
LVL 6

Expert Comment

by:neorush
ID: 13640527
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
}

?>
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question