PostQopen和PostQClose是日期时间列。 我需要一个php if语句来检查当前日期时间是否在PostQopen和PostQClose列之间。
$now = date('Y-m-d H:i:s');
if($rows['PostQopen'] >= '$now' && $rows['PostQClose'] < '$now' ){
echo "TRUE";
} else {
echo "FALSE";
};
答案 0 :(得分:7)
使用DateTime。
$date = new DateTime("now");
$PostQopen = new DateTime($rows['PostQopen']);
$PostQClose = new DateTime($rows['PostQClose']);
if($PostQopen >= $date && $PostQClose <=$now){
// do ur stuff
}
答案 1 :(得分:1)
从$ now中删除单引号并使用strtotime()
。如果仍然没有strtotime()
,则代码可以正常运行,因为如果它位于compare time
中,您可以datetime format
。
if(strtotime($rows['PostQopen']) >= strtotime($now) && strtotime($rows['PostQClose']) < strtotime($now) )
{
echo "TRUE";
}
else
{
echo "FALSE";
}
答案 2 :(得分:1)
找出使用DateTime
之间的函数在这里:
function dateIsBetween($from, $to, $date="now") {
$date = new \DateTime($date);
$from= new \DateTime($from);
$to = new \DateTime($to);
if ($date >= $from && $date <= $to) {
return true;
}
return false;
}
使用功能
dateIsBetween('2016-02-24', '2016-02-26', '2016-02-25'); //would return true
dateIsBetween('2016-02-24', '2016-02-26', 'now'); //would return true if the current date would be between specified to, from
dateIsBetween('2016-02-24', '2016-02-26'); //would return true if the current date would be between specified to, from
答案 3 :(得分:0)
$now = date('Y-m-d H:i:s');
if(strtotime($rows['PostQopen']) >= strtotime('$now') && strtotime($rows['PostQClose']) < strtotime('$now') ){
echo "TRUE";
} else {
echo "FALSE";
};
检查这是否有效,strtotime将给定时间转换为毫秒格式,它应该工作
答案 4 :(得分:0)
代替原生date()函数,最好使用DateTime对象,这是您的情况示例
现在()在PostQopen和PostQclose之间
$now = new DateTime(date('Y-m-d H:i:s'));
$DateRange = array(
'PostQopen' => new DateTime($rows['PostQopen']),
'PostQClose' => new DateTime($rows['PostQClose']),
);
if (($DateRange['PostQopen'] <= $now) && ($DateRange['PostQClose'] > $now)) {
echo "TRUE";
} else {
echo "FALSE";
}