Jump to content

Recommended Posts

I would like to delete all the records in mysql database that is older than 7 days.

 

How do i go about selecting it in the database ( currentdate - 7).

 

my database is ctrack1

table is operator

fields is: ID,Username,Password,Date_created,Level

 

Please assist  :)

Greatley appreciated!

 

Link to comment
https://forums.phpfreaks.com/topic/50457-solved-dates/
Share on other sites

Thank you for the quick response! you guys are magnificent.

 

How do i insert a datestamp via php? i am more familiar with Delphi and there i use date(now) function.

 

Please can you supply or point me in the right direction with php code.

 

Thank you verry much

Werner

Link to comment
https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-247942
Share on other sites

 

Good day to all that help me :)

 

i would like to thank you for your assistance so far. how ever the advice did not exactley help me yet. this is what i tried and it failed:

 

mysql_select_db($database_operator, $operator);

$query_delete = "DELETE FROM operator WHERE ((TO_DAYS(CURDATE() - TO_DAYS(Date_created)) > 7) AND Level='0'";

$delete = mysql_query($query_adminlogin) or die(mysql_error());

?>

 

the above code generates an error when i execute it stating that im using wrong version of MySQL. i am using for test purposes (as i know it works on my host) EasyPHP 1.8.

 

 

Then i tried the following :

 

$curdate = date("Y.m.d");

mysql_select_db($database_operator, $operator);

$query_cleaunup = "DELETE FROM operator WHERE '$curdate' - 'Date_created' < 7 AND Level='0'";

$cleaunup = mysql_query($query_cleaunup) or die(mysql_error());

 

 

When i execute this code it does nothing. but note that when i change the < sigh to > then it deletes all the Level 0 entries!

 

I just basically want to delete all the entries that is older than 7 days and is lovel 0 in my database. How do i do it? please i am desperate for an answer. I appreciate your response!

 

Link to comment
https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248758
Share on other sites

If you going to use this, then do like this!

 

 

or use time() to get the unix stamp for the date you create the record

use the time() when you modified the record or time() for now - you want to delete the record

or add a number of days to the date created

 

 

so you will have Date_created field in the database that will store time - it is a Unix time stamp

 

select Date_created from mysql

 

do $date_created = Date_created

 

use this to calculate how many days you need to delete

$h = 24 *number of days;

$hm = $h * 60;

$ms = $hm * 60;

 

So you will be deliting from $date_created to $date_created + $ms

 

 

$curdate =  mysql Date_created

mysql_select_db($database_operator, $operator);

$query_cleaunup = "DELETE FROM operator WHERE 'Date_created' >= $date_created AND 'Date_created' <= '$date_created + $ms'";

$cleaunup = mysql_query($query_cleaunup) or die(mysql_error());

 

 

You can look up the syntax for MySql and PHP if you are interested in using this technique.

 

Also learn how date() and strtotime() works.

 

Explore the full power of PHP!

Link to comment
https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248831
Share on other sites

Thank you.

 

I dont use timestamp though to save the date. insted i use this :

 

$curdate = date('Y.m.d');

 

so when i save into the database it saves the date in format = date('Y.m.d');

 

i tried to say :

 

delete from operator where ((date('Y.m.d') - 7) < 7 ) and level ='0'

 

but its not working.

 

Where does my whole problem lie? am i saving it wrong to to the database? date('Y.m.d') is a timestamp of today though.

 

Thank you  :)

 

Link to comment
https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248843
Share on other sites

 

Sorry that i ask so many dumb questions.

 

does this look better?

 

delete from operator where ((date('Y.m.d') - 'Date_Created')< 7 ) ;

 

cause this is what i used before and it only deleted if the sign was > instead of < altough there was dates in the records wich was smaller.(look below)

 

"DELETE FROM operator WHERE date('Y.m.d')- 'Date_created' < 7 AND Level='0'";

 

how do i get it to also only delete records with level 0?

 

Link to comment
https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248860
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.