如何检查日期是否因PHP中的数值而过期

时间:2013-02-14 22:31:37

标签: php date

嘿伙计们,让我们说我有一个成员放了20天。所以20天后如果有成员,让我们说没有登录就会改变那里的状态。但仅基于状态更改日期,该日期在MySQL数据库中写为year-month-day。因此,如果他的状态更改日期已经过了20天,那么他的状态将会改变。

如果你能帮我解释一下如何做到这一点,我将不胜感激!

大卫

更新:

代码:

$newStatus = "Non-Active - Driver Chose Non-Compliance";

    $sql = "SELECT username,ATF FROM members WHERE username = 'test'";
    $getcsvuser = $DBH->prepare($sql);
    $getcsvuser->execute();
    while($row = $getcsvuser->fetch(PDO::FETCH_ASSOC)){

        $memusername = $row['username'];
        $memATF = $row['ATF'];
        if ($memATF != 0 || $memATF != "0")
        {
        $tsql = "SELECT username,status,memberview ,statuschangedate FROM csvdata WHERE memberview =:user";
        $tgetcsvuser = $DBH->prepare($tsql);
        $tgetcsvuser->execute(array(':user' => $memusername));
        while($trow = $tgetcsvuser->fetch(PDO::FETCH_ASSOC)){

            $csvstatus = $trow['status'];
            $csvusername = $trow['username'];
            $csvdate = $trow['statuschangedate'];

            if($csvstatus == "Open" || $csvstatus == "Enrolled - Policyholder Follow-Up Required" || $csvstatus == "Enrolled - Employee Follow-Up Required" || $csvstatus == "Non-Active - Insurance Cancelled" || $csvstatus == "Non-Active, Unable to Monitor - Incidental Business use Exclusion" || $csvstatus == "Non-Active - Employee Not Covered Under Listed Policy" || $csvstatus == "Non-Active - PolicyHolder Cancelled Additional Interest")
            {

                $newsql = "UPDATE csvdata SET status = :newstatus WHERE statuschangedate < NOW() - INTERVAL :atf DAY AND username =:mem";
                $newgetcsvuser = $DBH->prepare($newsql);
                $newgetcsvuser->execute(array(':newstatus' => $newStatus, ':atf' => $memATF, ':mem' => $csvusername));
                while($rrow = $newgetcsvuser->fetch(PDO::FETCH_ASSOC)){
                    echo "working";
                }
            }

        }
        }

    }

3 个答案:

答案 0 :(得分:2)

它可以是两件事的组合:

mysql更新要更改的用户:

update
  members
set
  status = 'foo'
where
  status_change_date < NOW() - INTERVAL 20 DAY

第二部分是一个cron作业,每天运行该查询。

答案 1 :(得分:1)

怎么样:

UPDATE tablename
SET inactive = 1
WHERE date_field < DATE_SUB(NOW(), INTERVAL 20 DAY)

您可以每晚一次以cronjob身份运行此查询。

所以这可以在不使用PHP逻辑的情况下完成,除了执行此SQL查询。

答案 2 :(得分:1)

我没有对此进行测试,但它应该很接近。

//$dbVal = $row["databaseColumn"];
$dbVal = "2013-01-24";  //For example
$now = Date();

$diff = abs($now - strtotime($dbVal));

$daysSince = floor(($diff/(60*60*24));
if ($daysSince >20){
    //Set status in DB;
}

合并@popnoodles解决方案:

//$dbVal = $row["databaseColumn"];
$dbVal = "2013-01-24";  //For example
$expireTime = 20;

$diff = strtotime($dbVal) - strtotime("today - $expireTime days");
if ($diff < 0 ){
    //Set status in DB;
}