如何删除天数的数据>三十

时间:2012-05-10 06:01:44

标签: php

我是php新手 我有一张桌子

+-----------------------------------------------------------------+
|                              seeker                             |
+----+-------+----------+---------------+-------------------------+
| id | name  | password | register_date | login_date | limit_date |
+----+-------+----------+---------------+------------+------------+
| 01 | ali   |   ****   |  2012-02-08   | 2012-02-09 | 2012-03-09 |
| 02 | hamza |   ****   |  2012-04-10   | 0000-00-00 | 0000-00-00 |
| 03 | sam   |   ****   |  2012-04-15   | 0000-00-00 | 0000-00-00 |
+----+-------+----------+---------------+------------+------------+

我必须应用两个查询 一个在限制日期=当前日期时删除数据

当用户login_date是'0000-00-00'并且其(register_date - 当前日期)> = 30时删除数据

我的第一个查询是正确实施

在我的第二个查询中,我通过从当前日期减去register_date来找到没有天数,如果没有大于或等于30的天数则删除该用户。

我的问题是,当任何一个用户返回没有天数> = 30时,所有login_date ='0000-00-00'的用户都会被删除。  这是我的代码

    //code for  delete user where (current date - register date)>=30
    $qry="select * from seeker where login_date='0000-00-00'";
    $rs= mysql_query($qry, $con);
     $res=mysql_num_rows($rs);
    echo "total rows of zero dates "."".$res;
    echo "<br>";
    if($res!=0)
    {
        echo "user exist of date='0000-00-00'";
        echo"<br>";
        $qqq="select * from seeker where login_date='0000-00-00'";
            $rsq= mysql_query($qqq, $con);
            while($resq=mysql_fetch_array($rsq))
            //echo $resq;
        {
          $r=$resq['c_date'];
          echo $r;
          $reg_date = strtotime($r); 
          echo "<br>";

    //current date
        $c= date("Y-m-d");
        echo $c;
        //echo "current date".""."<br>";
        $current_date = strtotime($c); 
        echo "<br>";

         //date difference
          $datediff = $current_date - $reg_date;
          $days = floor($datediff/(60*60*24));
          echo "days"."".$days;
          echo "<br>";

        if($days>=30)
        {
        $qry1="delete from seeker where $days = '30'";
        $rs1= mysql_query($qry1, $con);
        echo $qry1;
        echo "<br>";
        echo "<br>";
        }
      }

}
// code for delete user where current date = limit date
$qry3="select * from seeker where current_date = limit_date";
$rs3= mysql_query($qry3, $con);
$res3=mysql_num_rows($rs3);
if($res3!=0)
{

        $q= "delete from seeker where current_date = limit_date";
        $rq=mysql_query($q, $con);
        echo "$q";
        echo "<br>";
}
请告诉我哪里弄错了。  我想只删除那些天> gt = 30的用户,但是它删除了login_date ='0000-00-00'的所有用户

for example if current date is 2012-01-10. it will not delete user
if current date is 2012-05-10.. it must delete only "hamza" but it delets "sam" too 

请帮助我,我是学生和PHP新手。提前谢谢

2 个答案:

答案 0 :(得分:1)

请在查询中尝试DATEDIFF(time1 ,time2)函数和NOW()函数,如下所示

DELETE FROM seeker WHERE DATEDIFF( NOW( ) , `register_date` )  >= 30 AND 
`login_date` = '0000-00-00'

感谢。

答案 1 :(得分:0)

使用以下查询

DELETE FROM `seeker` 
WHERE `register_date`>DATE_SUB(NOW(), INTERVAL 30 day)