...

View Full Version : Find difference in current time and time in db



golffor1
01-31-2012, 02:33 AM
Hello I have the following code





$ip = getenv("REMOTE_ADDR") ;
Echo "Your IP is " . $ip;
echo "<br>";

//find out if the ip address is inside the database
$doesIpExsist = mysql_query("Select * from login_information where IP_Address like '$ip'", $link);
$num_rows = mysql_num_rows($doesIpExsist);

echo "$num_rows Rows\n";

if ($num_rows == 0)
{
//create the field inside the database
$ip_query = "INSERT INTO login_information (IP_address, `Login_attempts`, Login_time, Total_login_attempts) VALUES ('$ip', '1', NOW(), '1')";
$ip_result = mysql_query($ip_query, $link) or die (mysql_error());
}
else //the fields alaready exsist
{
$curDate = date("Y-m-d H:i:s", time());

echo $curDate;



$select_ip_id = "SELECT id from login_information where IP_address like '$%ip'";
$ip_error = mysql_query($select_ip_id, $link) or die (mysql_error());
$get_ip_time = "Select Login_time from login_information where IP_address like '$ip'";
$get_ip_error = mysql_query($get_ip_time, $link) or die (mysql_error());

$sql = mysql_query("SELECT TIMEDIFF($curDate, $get_ip_error) AS difference FROM Login_time");
$result = mysql_fetch_array($sql, $link) or die mysql_error());
//echo $result['difference'];





//$timeDifference = time_diff ($get_p_error, $curDate);
//echo $timeDifference;
//$timeDifference = $get_ip_error - $curDate;
//echo "<br>";
//echo $timeDifference;
//mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'");

//$ip_query = "UPDATE INTO login_information (IP_address, `Login_attempts`, Login_time, Total_login_attempts) VALUES ('$ip', '1', NOW(), '1')";
echo "So far so good";


}
?>




But the problem is that I can't figure out how to subtract one date\time from another date\time.

Basically I want to stop people from hacking into pages...I figured the best way was to take the IP address of the person, count how many times they have looked at the login page. Then I would ban them for 5 minutes if they looked at the login page more than 5 times. The login page only ask for the username and password and nothing else.

mlseim
01-31-2012, 02:50 AM
Tell us how this is formatted: Login_time
Give an example of what one of those time values look like.

BluePanther
01-31-2012, 02:56 AM
http://www.codingforums.com/showthread.php?t=250156
^this :) Best solution is Fou-Lu's

golffor1
01-31-2012, 03:26 AM
One of the examples of the date\time is

2012-01-30 02:43:13

but some of them maybe days\months and possible years.

BluePanther
01-31-2012, 04:20 AM
One of the examples of the date\time is

2012-01-30 02:43:13

but some of them maybe days\months and possible years.

So the date times all take different formats? Different from YYYY-MM-DD HH:MM:SS? Why are they all different? You need to define a set standard for storing your data in order to make comparisons. Once you've done that, use the datetime object and dateinterval like Fou-Lu posted in the link I sent you.

You might want to consider just using a timestamp instead of a visual date and time combination, then using the dateinterval to give a better display.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum