我缺乏PHP / MySQL环境的一些经验。我有一个名为 MyTable 的MySQL表。在其中我有一个名为 DATETIME NOT NULL 的 RowTime 字段。在PHP中选择行后,我想检查 RowTime 是否超过3天。
鉴于所有不同类型的时间类型,有人可以帮助完成以下代码(我故意省略各种错误处理):
$dblink = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$dbquery = "SELECT RowTime FROM MyTable WHERE Id='" . $myId . "'";
$result = mysqli_query($dblink, $dbquery);
$numrows = mysqli_num_rows($result);
// ... Verify $numrows == 1 ...
$myRow = mysqli_fetch_assoc($result);
$rowTime = $myRow['RowTime'];
// NEED HELP HERE to check whether $rowTime is older or younger than 3 days
mysqli_free_result($result);
答案 0 :(得分:6)
您可以使用特殊的SQL运算符来检查您的日期是否超过3天:
$dblink = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$dbquery = "SELECT RowTime, IF(RowTime + INTERVAL 3 DAYS > now(), true, false) as isYounger FROM MyTable WHERE Id='" . $myId . "'";
$result = mysqli_query($dblink, $dbquery);
$numrows = mysqli_num_rows($result);
// ... Verify $numrows == 1 ...
$myRow = mysqli_fetch_assoc($result);
$rowTime = $myRow['RowTime'];
if($myRow['isYounger']) {
//record is younger
} else {
//record is older
}
mysqli_free_result($result);
答案 1 :(得分:1)
您可以使用以下查询:
SELECT RowTime,TIMEDIFF(NOW(),RowTime)>"72:00:00" AS "Old" FROM MyTable ;
这将引入专栏" Old"如果RowTime超过3天,则为1。否则它将为0.请注意,这并不考虑时区。