wernervantonder Posted May 8, 2007 Share Posted May 8, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/ Share on other sites More sharing options...
jitesh Posted May 8, 2007 Share Posted May 8, 2007 DELETE FROM `operator` WHERE ((TO_DAYS(CURDATE()) - TO_DAYS(Date_created)) > 7) Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-247937 Share on other sites More sharing options...
igor berger Posted May 8, 2007 Share Posted May 8, 2007 You may want to set up a UNIX date stamp field for each record for when the record was created another one when the record was updated etc.... Then you have more flexability to delete the records you want! Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-247938 Share on other sites More sharing options...
wernervantonder Posted May 8, 2007 Author Share Posted May 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-247942 Share on other sites More sharing options...
igor berger Posted May 8, 2007 Share Posted May 8, 2007 you can use date() with a special command for the unix stamp. Look up the syntax at www.php.net Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-247943 Share on other sites More sharing options...
jitesh Posted May 8, 2007 Share Posted May 8, 2007 http://in2.php.net/date Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-247945 Share on other sites More sharing options...
igor berger Posted May 8, 2007 Share Posted May 8, 2007 Service on demand. Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-247951 Share on other sites More sharing options...
wernervantonder Posted May 9, 2007 Author Share Posted May 9, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248758 Share on other sites More sharing options...
jitesh Posted May 9, 2007 Share Posted May 9, 2007 $query_delete = "DELETE FROM operator WHERE ((TO_DAYS(CURDATE()) - TO_DAYS(Date_created)) > 7) AND Level='0'"; Check paranthesis. --------------- OR $query_delete = "DELETE FROM operator WHERE (DATEDIFF(CURDATE(),Date_created) > 7) AND Level='0'"; Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248759 Share on other sites More sharing options...
igor berger Posted May 9, 2007 Share Posted May 9, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248831 Share on other sites More sharing options...
wernervantonder Posted May 9, 2007 Author Share Posted May 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248843 Share on other sites More sharing options...
igor berger Posted May 9, 2007 Share Posted May 9, 2007 compare time stamp to time stamp not 7 and 0 Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248856 Share on other sites More sharing options...
wernervantonder Posted May 9, 2007 Author Share Posted May 9, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248860 Share on other sites More sharing options...
jitesh Posted May 9, 2007 Share Posted May 9, 2007 $query_delete = "DELETE FROM operator WHERE ((TO_DAYS(CURDATE()) - TO_DAYS(Date_created)) > 7) AND Level='0'"; Check paranthesis. --------------- OR $query_delete = "DELETE FROM operator WHERE (DATEDIFF(CURDATE(),Date_created) > 7) AND Level='0'"; Have you try this ? Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248864 Share on other sites More sharing options...
wernervantonder Posted May 9, 2007 Author Share Posted May 9, 2007 no not yet. i have to test it but can only test in few hours time. so by tomorrow i shall let you know if i have success or not. Thank you for your assistance. Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-248869 Share on other sites More sharing options...
wernervantonder Posted May 10, 2007 Author Share Posted May 10, 2007 Thank you very much ! SOLVED! Quote Link to comment https://forums.phpfreaks.com/topic/50457-solved-dates/#findComment-249564 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.