我正在尝试检查我的字符串$何时根据当前时间()确保它不是30天+未来,但也不低于当前时间()。出于某种原因,strtotime正在引发某种问题。关于如何使这个脚本起作用的任何建议?
<?php
$when = '2011/07/11 11:22:52';
if ($when > strtotime('+30 days', time()));
{
echo "too far into the future";
header('Refresh: 10; URL=page.php');
die();
}
if ($when < time());
{
echo "less than current time";
header('Refresh: 10; URL=page.php');
die();
}
echo "pass";
header('Refresh: 10; URL=page.php');
die();
?>
答案 0 :(得分:3)
您的问题是您正在将日期字符串与Unix时间戳进行比较。在进行比较之前,您需要将$when
转换为Unix时间戳:
$when = strtotime('2011-07-11 11:22:52');
我发现使用DateTime()
使这简单易读(并且还可以处理夏令时等):
$when = new DateTime('2011-07-11 11:22:52');
$now = new DateTime();
$future = new DateTime('+30 days');
if ($when > $future )
{
echo "too far into the future";
header('Refresh: 10; URL=page.php');
die();
}
if ($when < $now)
{
echo "less than current time";
header('Refresh: 10; URL=page.php');
die();
}
echo "pass";
header('Refresh: 10; URL=page.php');
die();