0

我正在使用 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 天内,我认为它与语句中的“<”或日期的格式有关?

谢谢,西蒙

4

2 回答 2

1

您想比较时间戳:

<?php
if (strtotime($post['created_on']) >= strtotime('-7 days'))
{
    // Created in the last seven days
}
于 2013-03-08T22:31:47.153 回答
0

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

请注意,Fri ...它低于字母表中Sat ...的比较。FS

使用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
}
于 2013-03-08T22:31:12.453 回答