我得到了以下日期:
$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');
}但是我一直被重定向到一个用户应该继续投票的页面。
我做错了什么?
提前感谢您的帮助
发布于 2016-09-17 11:13:01
首先,我可以看到你的代码中的错误是
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');
}如果这对你有效,请告诉我
发布于 2016-09-17 11:04:12
strtotime()返回一个时间戳(从the epoch开始的整数秒计数),您正在将其与格式化的日期字符串(由date ()返回)进行比较。苹果和桔子。
请改用:
if( time() > strtotime(......which将一个时间戳(由time()返回)与另一个时间戳(由strtotime()返回)进行比较。
https://stackoverflow.com/questions/39542452
复制相似问题