PHP的日期超过1天

时间:2012-08-17 23:11:34

标签: php mysql date

我需要检查PHP中特定时间的日期是否超过1天。我尝试了下面的代码,但似乎无法正常工作。下面的代码似乎不起作用。这实际上引出了两个问题。

1)为什么下面的代码不起作用?

2)这种方法有效还是应该研究其他方法?

if (date("Y-m-j H:i:s", strtotime('1 day ago')) < date("Y-m-j H:i:s", strtotime($act['lpwdchange']))) {
    // Code Here
}

谢谢!

2 个答案:

答案 0 :(得分:12)

使用否定......

strtotime('-1 day')

但如果您已将时间存储在$act['lpwdchange']

if (strtotime('-1 day') < strtotime($act['lpwdchange'])) {
    // Code Here
}

答案 1 :(得分:3)

比较日期的最简单方法可能是将它们转换为时间戳然后比较那些:

if(strtotime('-1 day') < strtotime($act['lpwdchange']));

你的工作不起作用的原因可能是因为它正在比较字符串,这可能会导致未定义的行为。

相关问题