Time Difference

Hello, I need code to search through a database that will compare a row called 'timeInserted' to the current time if the difference between the two is more than 1min, I need to remove the record from the database.

Please provide code to do so.

Brian
LVL 19
BrianGEFF719Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
delete yourtable
where timeInserted < DATE_SUB(CURDATE(),INTERVAL -1 MINUTE);
Guy Hengel [angelIII / a3]Billing EngineerCommented:
or better, to check against the time :
delete yourtable
where timeInserted < DATE_SUB(CURTIME(),INTERVAL -1 MINUTE);
star_trekCommented:
try
delete yourtable where timeInserted < DATE_SUB(NOW(),INTERVAL -1 MINUTE);
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

BrianGEFF719Author Commented:
angelIII, is this a SQL statment?

brian
BrianGEFF719Author Commented:
can I add WHERE statments afterwards?
star_trekCommented:
in php
<?
mysql_connect(HOST,USERNAME,PASSWORD);
@mysql_select_db(DATABASE) or die( "Unable to select database");

$sql = "Delete <table-name> where `timeInserted` < DATE_SUB(NOW(),INTERVAL -1 MINUTE)";
$result=mysql_query($sql);
if($result) {
    //deletion successful
} else {
   //unsuccessful
}
mysql_close();
?>
BrianGEFF719Author Commented:
Does not work.


Insert like this ->
     $theTime = date("M j Y G:i:s");
     $varSQL = "INSERT INTO `tech` ( `techName` , `techIP`, `sessionID`, `port`, `lastUpdate` ) VALUES ('$name', '$techIP', '$ses', '$port', '$theTime')";
 

Check for old line this->

 $sql = "Delete `tech` where `lastUpdate` < DATE_SUB(NOW(),INTERVAL -3 MINUTE)";
 $result2 = mysql_query($sql, $dbh);
star_trekCommented:
Try this
$sql = "Delete `tech` where DATE_FORMAT(`lastUpdate`,GET_FORMAT(DATETIME,'ISO')) < DATE_SUB(NOW(),INTERVAL -3 MINUTE)";
BrianGEFF719Author Commented:
still doesnt work
BrianGEFF719Author Commented:
It does nothing.
BrianGEFF719Author Commented:
lastUpdate is varchar(100)


Brian
BrianGEFF719Author Commented:
Is there a way I can do this via PHP to atleast beable to evaluate the output?

Brian
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>angelIII, is this a SQL statment?
yes

>Does not work.
this is because of : lastUpdate is varchar(100)
how the "hell" did you come to make a datetime value into a varchar column.
get that column changed to datetime, and the delete will work correctly
BrianGEFF719Author Commented:
>>this is because of : lastUpdate is varchar(100)
>>how the "hell" did you come to make a datetime value into a varchar column.
>>get that column changed to datetime, and the delete will work correctly


LOL...that post made my night...will fix asap.

BrianGEFF719Author Commented:
AngelIII: That fixed that problem.

However, how do I update a record without using a string time that I generated. For example, one of the col's in the row is IP and one is lastUpdate.

I want to update IP with a string and lastUpdate I want to be the current Time. As you saw above I was using a string with the php date() function to do this.


-Brian
Guy Hengel [angelIII / a3]Billing EngineerCommented:
UPDATE yourtable set lastupdate = NOW() where ...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
star_trekCommented:
Brian, what delete query you used?
BrianGEFF719Author Commented:
Once it was changed to a datetime field, angelIII's first post worked just fine.


Brian
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.