Check MySQL DATETIME in PHP

I lack experience in the PHP / MySQL environment. I have a MySQL table called MyTable. In it, I have a field called RowTime of type DATETIME NOT NULL. After selecting a string in PHP, I want to check if RowTime is older or younger than 3 days.

Given all types of time types, someone can help execute the following code (I intentionally prevent the processing of various errors):

$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);
+4
source share
2 answers

You can use a special SQL statement to verify that your date is older than 3 days:

$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);
+6
source

You can use the following query:

SELECT RowTime,TIMEDIFF(NOW(),RowTime)>"72:00:00" AS "Old" FROM MyTable ;

"", 1, RowTime 3 . 0. , .

+1

Source: https://habr.com/ru/post/1534249/


All Articles