简体   繁体   中英

Checking if date in database is greater than current time

I have a PHP script to check if the date stored in the database is greater, equal or less than the current time.

  • If it is greater or equal, it echos 'greater'.
  • If it isn't greater, it echoes 'not'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM