2011-08-20 45 views

回答

100

尝试使用这样的事情:

if(strtotime('2011-08-19 17:14:40') < strtotime('-30 days')) { 
    // this is true 
} 

此外,这个字符串看起来是存储在SQL的日期时间/时间戳字段。您可以使用直接选择从旧日期,数据库中的所有条目:

SELECT ... WHERE `datetime` + INTERVAL 30 DAY < NOW() 
+1

其有用的,+1它:) –

3
strtotime('2011-08-19 17:14:40') + 30 * 24 * 60 * 60 < time(); 
+0

它应该是'strtotime('2011-08-19 17:14:40')+ 30 * 24 * 60 * 60 <时间();'更正它。 – nobody

+3

@nobody你有> 2000代表。你可以自己改正,就像我一样。 :) – AgentConundrum

+3

@ gion_13做这些'24 * 60 * 60'计算通常是一个糟糕的主意,因为在处理夏令时等事情时它们可能会变得不准确。最好使用RiaD建议的东西,并让'strtotime()'处理相对时间计算。 – AgentConundrum

7

您可以使用碳如下

if (30 - ((new \Carbon\Carbon($given_date, 'UTC'))->diffInDays()) < 0) { 
    echo "The date is older than 30 days"; 
} 
7

如果您在PHP 5.3或更高版本,你可以这样做:

$someDate = new \DateTime('2011-08-19 17:14:40'); 
$now = new \DateTime(); 

if($someDate->diff($now)->days > 30) { 
    echo 'The date was more than 30 days ago.'; 
} 
+0

可以链接到diff($ now) - > days的文档吗?我想调整它几个小时而不是几天。谢谢。 – Jay42

+0

[diff](http://php.net/manual/en/datetime.diff.php)返回[DateInterval](http://php.net/manual/en/class.dateinterval.php)。 DateInterval有一个'h'属性用于'小时',但它实际上是差异的一部分(因为它涉及y(年),m(月),d(天)等)。为了获得数小时,我会做'($ now-> getTimestamp() - $ someDate-> getTimestamp())/ 3600'。这将返回一个小数。你可以选择使用[round()](http://php.net/manual/en/function.round.php),[intval()](http://php.net/manual/en/function。 intval.php)等,如果你想要它作为一个整数。 –

相关问题