2016-09-17 25 views
0

我給出的日期如下:日期比較:根據日期和時間重定向到頁面。這不是工作

$startdate = '09/16/2016 07:00:00 AM'; 
$enddate = '10/14/2016 11:59:59 PM'; 

,我被告知,如果當前的日期是那些日期間,用戶會被重定向到一個名爲vote.php頁面,他們areable登錄系統並投票。

但是,如果當前日期不在這些日期之內,請將用戶重定向到名爲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'); 
} 

但我一直得到重定向到用戶都應該繼續投票頁面。

我在做什麼錯?

預先感謝您的幫助

回答

0

首先,我可以看到錯誤在你的代碼

header('location:vote.php'); 

永遠不會退出堂妹運行在該行之前被調用。

我對這個問題的處理方法是,使用三個時間戳而不是日期。讓有這些變量,

$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'); 
} 

讓我知道這對你的作品

+1

非常感謝你。你的工作。 – Tairoc

0

的strtotime()返回一個時間戳(自the epoch秒的整數計數),其中你比較格式化的日期字符串(按日期()返回)。蘋果和桔子。

而是使用:

if(time() > strtotime(... 

...其中一個比較時間戳(按時間()返回)到另一個(通過的strtotime()返回)。