我得到了以下日期:
$startdate = '09/16/2016 07:00:00 AM';
$enddate = '10/14/2016 11:59:59 PM';
和我被告知,如果当前日期在这些日期之间,用户将被重定向到一个名为vote.php的页面,在那里他们可以登录系统并投票。
但是,如果当前日期不在这些日期之内,则将用户重定向到一个名为done.php的页面,告诉他们投票结束。
我一直在尝试以下代码:
$now = date("m/d/Y h:i:s A");
echo $now;
$startdate = '09/16/2016 07:00:00 AM';
$enddate = '10/14/2016 11:59:59 PM';
if ($now < strtotime($startdate) && $now > strtotime($enddate)) {
header('location:done.php');
exit;
header('location:vote.php');
}
但是我总是被重定向到一个用户应该继续投票的页面。
我做错了什么?
提前感谢您的协助
首先我可以看到你的代码中的错误
header('location:vote.php');
永远不会运行,因为在该行之前调用了exit。
我解决这个问题的方法是,使用三个时间戳代替日期。我们用这些变量
$startDate= mktime(7,0,0,9,16,2016);
$endDate= mktime(23,59,59,10,14,2016);
$now=$_SERVER['REQUEST_TIME'];
if ($now > $startDate && $now < $endDate) {
header('location:vote.php');
}
else{
header('location:done.php');
}
让我知道这是否适合你
strtotime()返回一个时间戳(从epoch开始的秒数整数),您要将其与格式化的日期字符串(由date()返回)进行比较。苹果和橘子。
而不是使用:
if( time() > strtotime(...
…它比较一个时间戳(由time()返回)和另一个时间戳(由strtotime()返回)。