PHP检查事件是否已启动
Im使用以下mysql脚本显示活动/即将到来的装置列表PHP检查事件是否已启动,php,mysql,Php,Mysql,Im使用以下mysql脚本显示活动/即将到来的装置列表 Select * FROM schedule WHERE schedule.gameDate > CURDATE() OR ( schedule.gameDate = CURDATE() and schedule.gameTime > CURTIME() ) GROUP BY schedule.tournament
Select * FROM schedule
WHERE schedule.gameDate > CURDATE() OR
( schedule.gameDate = CURDATE() and schedule.gameTime > CURTIME() )
GROUP BY schedule.tournament
ORDER By schedule.gameDate
上面的脚本非常好用
但是,作为防止用户访问已过期设备的附加检查,我将执行以下操作
$curTime = date('H:i:s');
$curDate = date('Y-m-d');
$sql = "SELECT * FROM schedule
WHERE tournament = :tour AND weekNum = :round";
$stmnt = $db->prepare($sql);
$stmnt->bindValue(':tour',$tournament);
$stmnt->bindValue(':round', $round);
$stmnt->execute();
$results = $stmnt->fetchAll();
foreach($results as $result){
echo $eventDate = $result['gameDate'];
echo $startTime = $result['gameTime'];
if($curDate > $eventDate){
echo '<h1> CURRENT ROUND ALLREADY STARTED</h1>';
die();
}//if
else if($eventDate==$curDate && $curTime>$startTime){
echo '<h1> CURRENT ROUND ALLREADY STARTED</h1>';
die();
}//else if
}//foreach
我意识到这不是最漂亮的代码,但根据我的经验和逻辑,如果声明
您可以将它们转换为DateTime对象或时间戳进行比较。字符串比较只会检查它们是否相等 因此:
然后,您可以检查日期是否相等或更晚,就像您已经在做的一样。请参阅php网站的链接。例如,
$now>$event
等。您可以将它们转换为DateTime对象或时间戳进行比较。字符串比较只会检查它们是否相等
因此:
然后,您可以检查日期是否相等或更晚,就像您已经在做的一样。请参阅php网站的链接。例如,$now>$event
,等等。在php中比较两个日期时使用strotime()
将if($curDate>$eventDate)
替换为if(strotime($curDate)>strotime($eventDate))
和其他比较也使用strotime()
在php中比较两个日期
将
if($curDate>$eventDate)
替换为if(strotime($curDate)>strotime($eventDate))
和其他比较也它应该是elseif,而不是else if。else和if之间不应该有空格 它应该是elseif,而不是elseif。else和if之间不应该有空格 在比较之前,尝试将它们转换为时间戳。我认为比较这样的字符串不会得到有意义的结果。在比较之前,请尝试将它们转换为时间戳。我认为比较这样的字符串不会得到有意义的结果。
$curTime = 09:30:00
$curDate = 2017-19-03
$eventDate = 2017-03-21
$startTime = 13:00:00
$now = new DateTime();
$event = new DateTime($eventDate . ' ' . $startTime);