我有一个foreach循环,我需要为可选的开始日期和到期日期添加日期条件。
我使用它是因为我需要设置一些日程安排,但我无法运行它。
$thedate = date("Y-m-d H:i:s");
foreach ( $getframe as $frame ){
if(isset($frame['dateexpire']) && $frame['dateexpire'] <= $thedate || $frame['dateexpire'] == NULL
|| isset($frame['datestart']) && $frame['datestart'] >= $thedate || $frame['datestart'] == NULL ) {
// get the frames
}
}
CCD_ 1和CCD_ 2要么为NULL,要么在我的表中设置为时间戳。
因此,如果开始日期为NULL或大于当前日期,或者到期日期为NULL或者小于当前日期,则应该显示该帧。
我想我完全错了,有人能阐明正确的方法吗?
您的错误是将时间戳($frames['datestart']
和$frames['dateexpire']
)与字符串($thedate = date("Y-m-d H:i:s");
)进行比较。
这是一个有一些额外改进的固定版本。
$today = time();
foreach ( $getframe as $frame ){
if (is_null($frame['datestart']) || is_null($frame['dateexpire']) || $frame['datestart'] > $today || $frame['dateexpire'] < $today)
// get the frames
}
}
- 我也将今天表示的变量命名为
$today
,这是最有意义的 - 我使用
is_null()
检查空值