我正在尝试制作一个网页,处理研讨会的帖子。我希望页面删除过期的研讨会(或研讨会发生的日期)我的表有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循环中,所以它可以从表中抓取每一行。但是即使delete语句不起作用,它也不会显示研讨会,对吗?顺便说一下,最后一条else语句是显示研讨会开始的地方。
如果有人知道更好的方法,我将不胜感激。我已经在这个问题上挣扎了一段时间。
发布于 2013-09-28 01:01:45
尽管将表重新设计为包含单个日期列会更好,但您可以像这样使用现有的模式:
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
}发布于 2013-09-28 00:47:05
您不必在一个月、一天和一年中各有三列。相反,您应该只有一个列类型为DATETIME的列。这将使您的工作变得更容易。只需与当前日期匹配即可。
发布于 2013-09-28 01:02:08
你可以这样做:
$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
}https://stackoverflow.com/questions/19055806
复制相似问题