Jump to content

time


jkkenzie

Recommended Posts

I would like to capture time when the record was saved  and store it in the database the later use it to delete records older that 10 minute or less.

 

Any guide would be appreciated.

 

Is this the right syntax or how best can i do it?

 

 $query= "DELETE FROM tblcalc WHERE  '$currenttime'-time >'10'"

 

thanks

Joseph

Link to comment
Share on other sites

Store the time() of the entry in the database in a field called something like 'saved_time'.

Then delete it like so:

 

$deleteTime = time() - (10 * 60); // 10 is the number of minutes.

$query = "DELETE FROM tblcalc WHERE saved_time<='" . $deleteTime . "'";

 

Pretty sure I got it the right way. :)

Link to comment
Share on other sites

Nothing is wrong. As stated in manual time() function returns current UNIX timestamp, that is number of seconds passed since Jan 1st 1970. To insert it into database, you need to convert it to one of the formats accepted by mysql. You can do it on PHP side using date() function, or on MySQL side using FROM_UNIXTIME() function.

Link to comment
Share on other sites

You either need to use a mysql DATETIME or a Unix timestamp. Just using a mysql TIME type won't address the rollover that happens at midnight.

 

A mysql DATETIME data type would be your best option because it will allow you to directly use the mysql datetime functions to do anything you want.

 

To store or update the current DATETIME, just use the mysql NOW() function in your query.

 

To DELETE records older than some time INTERVAL -

 

$limit = 10;
$query = "DELETE FROM your_table WHERE your_datetime_column < DATE_SUB(NOW(),INTERVAL $limit MINUTE)";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.