[英]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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.