PHP检查日期过去


PHP Checking Date Past

我正在使用RFC822日期格式,并试图让我的if语句工作,但它不会,我无法找出原因,这就是数据的响应:

$currentdate = Fri, 01 Mar 13 22:24:02 +0000
$post['created_on'] = Sat, 17 Nov 2012 19:26:46 +0100

这是我的声明:

$currentdate = date(DATE_RFC822, strtotime("-7 days"));
if ($post['created_on'] < $currentdate) 
{
  echo "test";
}
else
{
}

我试图检查上创建的数组是否在过去7天内,我认为它与语句中的"<"或日期格式有关?

谢谢,西蒙。

您想要比较时间戳:

<?php
if (strtotime($post['created_on']) >= strtotime('-7 days'))
{
    // Created in the last seven days
}

您的代码在进行字母数字比较时无法工作。RFC822不是为此设计的。

注意,Fri ...低于Sat ...是由于F在字母表中排在S之前。

使用DateTime类:

$currentdate = new DateTime('-7days +0100'); // ! use the same tz offset as the post !
$postdate = new DateTime('Sat, 17 Nov 2012 19:26:46 +0100');
if($postdate < $currentdate) {
  // ... do stufff
}