Discussion:
How to delete 3 months old records in my database?
Karl-Arne Gjersøyen
2013-08-02 10:58:41 UTC
Permalink
Hello again, folks!
I wish to delete records in my database that is older than 3 months.

$todays_date = date('Y-m-d');
$old_records_to_delete = ???

if($old_records_to_delete){
include(connect.php);
$sql = "DELETE FROM table WHERE date >= '$old_records_to_delete'";
mysql_query($sql, $connect_db) or die(mysql_error());
}

Thank you very much for your help to understand also this question :)

Karl
Dušan Novaković
2013-08-02 11:03:53 UTC
Permalink
$query = "DELECT FROM `__table_name__` WHERE `__date__` BETWEEN NOW() -
INTERVAL 3 MONTH AND NOW()"
Post by Karl-Arne Gjersøyen
Hello again, folks!
I wish to delete records in my database that is older than 3 months.
$todays_date = date('Y-m-d');
$old_records_to_delete = ???
if($old_records_to_delete){
include(connect.php);
$sql = "DELETE FROM table WHERE date >= '$old_records_to_delete'";
mysql_query($sql, $connect_db) or die(mysql_error());
}
Thank you very much for your help to understand also this question :)
Karl
--
mob: + 46 7 230 230 19
web: http://novakovicdusan.com

Please consider the environment before printing this email.
Karl-Arne Gjersøyen
2013-08-02 12:02:10 UTC
Permalink
Post by Dušan Novaković
$query = "DELECT FROM `__table_name__` WHERE `__date__` BETWEEN NOW() -
INTERVAL 3 MONTH AND NOW()"
This delete everything from now and 3months backwards. I want to store 3
months from now and delete OLDER than 3 months old records.

Karl
Simon Schick
2013-08-02 12:35:52 UTC
Permalink
Post by Karl-Arne Gjersøyen
Post by Dušan Novaković
$query = "DELECT FROM `__table_name__` WHERE `__date__` BETWEEN NOW() -
INTERVAL 3 MONTH AND NOW()"
This delete everything from now and 3months backwards. I want to store 3
months from now and delete OLDER than 3 months old records.
Karl
Hi, Karl

You're right, but restructuring, to get it the way you want, isn't be
that hard, is it? :)

$query = "DELETE FROM `__table_name__` WHERE `__date__` < NOW() -
INTERVAL 3 MONTH"

@Dusan,
Btw: What is "DELECT"? I assume it should've been "DELETE", right?

Bye
Simon
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Dušan Novaković
2013-08-02 13:01:22 UTC
Permalink
Yeah, just spelling mistake :-)
And yes, it should be:

$query = "DELETE FROM `__table_name__` WHERE `__date__` <= NOW() - INTERVAL
3 MONTH"


Cheers ;-)
Post by Simon Schick
Post by Karl-Arne Gjersøyen
Post by Dušan Novaković
$query = "DELECT FROM `__table_name__` WHERE `__date__` BETWEEN NOW() -
INTERVAL 3 MONTH AND NOW()"
This delete everything from now and 3months backwards. I want to store 3
months from now and delete OLDER than 3 months old records.
Karl
Hi, Karl
You're right, but restructuring, to get it the way you want, isn't be
that hard, is it? :)
$query = "DELETE FROM `__table_name__` WHERE `__date__` < NOW() -
INTERVAL 3 MONTH"
@Dusan,
Btw: What is "DELECT"? I assume it should've been "DELETE", right?
Bye
Simon
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
mob: + 46 7 230 230 19
web: http://novakovicdusan.com

Please consider the environment before printing this email.
Simon Griffiths
2013-08-02 11:13:24 UTC
Permalink
Hello,

Try something like:

$oldDate = new DateTime();
$oldDate->sub(new DateInterval('P3M'));
$old_records_to_delete = $oldDate->format('Y-m-d');


Hope this helps,

Si

Sent from my iPhone
Post by Karl-Arne Gjersøyen
Hello again, folks!
I wish to delete records in my database that is older than 3 months.
$todays_date = date('Y-m-d');
$old_records_to_delete = ???
if($old_records_to_delete){
include(connect.php);
$sql = "DELETE FROM table WHERE date >= '$old_records_to_delete'";
mysql_query($sql, $connect_db) or die(mysql_error());
}
Thank you very much for your help to understand also this question :)
Karl
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2013-08-02 13:31:57 UTC
Permalink
Post by Karl-Arne Gjersøyen
Hello again, folks!
I wish to delete records in my database that is older than 3 months.
$todays_date = date('Y-m-d');
$old_records_to_delete = ???
if($old_records_to_delete){
include(connect.php);
$sql = "DELETE FROM table WHERE date >= '$old_records_to_delete'";
mysql_query($sql, $connect_db) or die(mysql_error());
}
Thank you very much for your help to understand also this question :)
Karl
So close! BUT - you need to reverse your test.

where date <= '$old_records_to_delete'
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...