我的预期目标是让它查看帖子是否允许评论,如果允许,然后检查以确保date_comments_expires不在过去的一天之前。我不太确定如何结束这个if语句。任何帮助吗?
if ($mainNews[0]['allow_comments'] == 'Yes' AND $mainNews[0]['date_comments_expire'] == ) {
编辑:这是我更新的代码中,我得到一个调用成员函数getTimeStamp的非对象
if ($mainNews[0]['allow_comments'] == 'Yes' AND $mainNews[0]['date_comments_expire']->getTimestamp() > time() ) {
echo "<a href='"#'"></a><span>".$mainNews[0]['number_of_comments']."</span>";
}
这完全取决于数组的date_comments_expire
索引中包含的内容。我希望它要么是一个unix时间戳,说明注释何时过期,要么是一个合理的文本解释。
如果您正在使用unix时间戳,那么您做得很好。如果它是文本,那么您需要在继续之前将其转换为unix时间戳。最好的函数是strtotime()
。它可以解析各种文本日期时间表示,并将返回一个unix时间戳作为结果。
将端点表示为时间戳后,可以将其与当前时间进行比较。为此,您可以使用time()
函数,它以unix时间戳的形式返回当前时间。
由于unix时间戳只是整数(特别是从1970年1月1日开始的秒数),因此这是一个简单的比较。最后,您的代码看起来像这样:
// convert to timestamp if necessary, remove if unneeded
$commentExpiry == strtotime($mainNews[0]['date_comments_expire']);
if ($mainNews[0]['allow_comments'] == 'Yes' AND
$commentExpiry > time()) {
//submit comments
} else {
//error handling
}