I have a PHP script to check if the date stored in the database is greater, equal or less than the current time.
My issue is that even if the time in the database is not greater or equal to the current time, it echos 'greater' (when it's supposed to echo 'not').
Date stored in the database:
date_default_timezone_set('Europe/London');
$time_stamp = date('Y-m-d H:i:s');
$stmt = $con->prepare("SELECT * FROM table");
$stmt->execute();
$row = $stmt->fetch();
if($row['BannedUntil'] <= $time_stamp) {
echo 'Greater';
}else{
echo 'not';
}
Use MySQL Date and Time Functions DATEDIFF
date_default_timezone_set('Europe/London');
$time_stamp = date('Y-m-d H:i:s');
$stmt = $con->prepare("SELECT DATEDIFF(`BannedUntil`,NOW()) AS `daysUntil` FROM table");
$stmt->execute();
$row = $stmt->fetch();
if($row['daysUntil'] >= 0) {
echo 'Greater'; // banned
}else{
echo 'not'; // not banned
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.