unix时间戳之间的时差

时间:2010-06-27 18:20:50

标签: php

如果unix时间戳在当前日期的21天到49天之间,我必须尝试解决。任何人都可以帮我解决这个问题吗?谢谢!

3 个答案:

答案 0 :(得分:5)

欢迎来到 SO
这应该这样做:

if (($timestamp > time() + 1814400) && ($timestamp < time() + 4233600)) {
 // date is between 21 and 49 days in the FUTURE
}

这可以简化,但我想你想看一个更详细的例子:)

我从1814400获得21*24*60*60,从4233600获得41*24*60*60

编辑:我假设未来日期。另请注意,自PHP中的Epoch以来,time()返回(而不是毫秒)。

这是您在过去(自您编辑问题后)的方式:

if (($timestamp > time() - 4233600) && ($timestamp < time() - 1814400)) {
 // date is between 21 and 49 days in the PAST
}

答案 1 :(得分:3)

PHP5 DateTime类非常适合这类任务。

$current = new DateTime();
$comparator = new DateTime($unixTimestamp);
$boundary1 = new DateTime();
$boundary2 = new DateTime();

$boundary1->modify('-49 day'); // 49 days in the past
$boundary2->modify('-21 day'); // 21 days in the past

if ($comparator > $boundary1 && $comparator < $boundary2) {
    // given timestamp is between 49 and 21 days from now
}

答案 2 :(得分:3)

strtotime在这些情况下非常有用,因为你几乎可以说自然英语。

$ts; // timestamp to check
$d21 = strtotime('-21 days');
$d49 = strtotime('-49 days');

if ($d21 > $ts && $ts > $d49) {
    echo "Your timestamp ", $ts, " is between 21 and 49 days from now.";
}