2015-04-17 83 views
0

現在這是我的代碼包含PHP時間如果擴展

的index.html

<form action="timeout.php" method="POST"> 
<input type="time" name="txt_time" required> 
<input type="submit"> 
</form> 

timeout.php

<?php 
$formattedTime = date("h:i A", strtotime($_POST['txt_time'])); 
$curfew = date("h:i A", strtotime('20:00')); 
if($formattedTime > $curfew) { 
echo "You are past the curfew time"; 
} else { 
echo "Success"; 
} 
?> 

我的問題是,即使我把對exampole 08 :當天上午00時,它仍然說你已經過了宵禁時間。有什麼想法嗎?

+0

所以,如果輸入的是「3:00 PM」或「14:00」要評估,如果那個時候是前一套宵禁?不要使用'str2time()'。 – Twisty

+0

''?在所有瀏覽器上都不支持 – Ghost

+0

@Twisty輸入類型是時間,那麼我應該使用什麼? – FewFlyBy

回答

2

date()函數格式化時間戳爲一個字符串,當你比較使用$formattedTime > $curfew PHP轉換兩個字符串爲整數,這將使類似8:41 AM簡單8兩個字符串。

下面是一個簡單草率的黑客應該工作正常

<?php 
$formattedTime = intval(date("Hi", strtotime($_POST['txt_time']))); 
$curfew = intval(date("Hi", strtotime('20:00'))); 
//'H' will get the hour of the time from 0-23, intval will convert that to an integer 

if($formattedTime > $curfew) { 
    echo "You are past the curfew time"; 
} else { 
    echo "Success"; 
} 
?> 

Example

+0

我會試試這個,並回復給你一點謝謝 – FewFlyBy

+0

你好這個工程。現在我的問題是每當我在8點到9點之間選擇一個時間像8:01-8:59它仍然回聲的成功,我想它回聲「你已經過了宵禁時間」; – FewFlyBy

+0

您可能在'date()'中只用'H'來使用舊代碼,而用'Hi'來代替,這佔了幾分鐘的時間。如果你想盡量檢查確切的第二個,那就使用'His'。 – AlphaDelta