根据当前日期和表日期删除mysql表行

时间:2013-09-27 16:31:40

标签: php mysql date if-statement sql-delete

我正在尝试制作一个处理研讨会帖子的网页。我希望该页面删除超过其截止日期(或研讨会发生日期)的研讨会 我的桌子有3个日期位,一个月,一个一天,一个一年。它们都是表中的整数。

if(date("Y") > $info['year'])  //if the year is old
{ $sql = "DELETE FROM seminars WHERE ID = '$ID'";
  mysql_query($sql); } //then delete this seminar
 else if(date("Y") == $info['year'] && date("n") > $info['month']) //if we are in the year of the seminar, but the month is now old
    { $sql = "DELETE FROM seminars WHERE ID = '$ID'"; 
      mysql_query($sql);} // then delete this seminar

else if(date("Y") == $info['year'] && date("n") == $info['month'] && date("j") > $info['day'])  //if we are in the year of the seminar and the month of the seminar, but the day is old
    { $sql = "DELETE FROM seminars WHERE ID = '$ID'";
      mysql_query($sql); }  // then delete this seminar
 else // if there is no reason to delete the seminar then print out this seminar

从上面可以看出,我试图将系统时间用于年月日,并比较每一个以查看是否有任何旧的。但是,它永远不会删除研讨会。

现在,$info是表行,因为此语句位于while循环中,因此它可以从表中获取每一行。但即使删除声明不起作用,它也不会显示研讨会对吗?最后一个声明就是在那里开始展示研讨会。

如果有人知道更好的方法,我会很感激。我一直在努力解决这个问题。

3 个答案:

答案 0 :(得分:1)

虽然将表重新设计为包含单个日期列会更好,但您可以使用现有的模式,如下所示:

if (date("Y-m-d") > sprintf("%04d-%02d-%02d", $info['year'], $info['month'], $info['day'])) {
    $sql = "DELETE FROM seminars WHERE ID = '$ID'"; 
    mysql_query($sql);} // then delete this seminar
}

答案 1 :(得分:0)

您不必每个月,每天和每年都有三列。相反,您应该只有一列具有列类型DATETIME的列。这将使您的工作更轻松。您只需要与当前日期匹配。

答案 2 :(得分:0)

您可以执行以下操作:

$infodate = strtotime(implode('-', array($info['year'], $info['month'], $info['day'])));

if (time() > $infodate) {
    mysql_query("DELETE FROM seminars WHERE ID = '$ID'")
} else {
    //print out this seminar
}
相关问题