2015-05-27 38 views
1

我的按鈕上寫着Accept,從創建時開始應該僅對72 HOURS有效,而在過去的72時,它應該自動禁用,並在<span>中發出消息。如何使用PHP和jQuery動態設置按鈕的到期時間?

PHP:

這是我的時間收集變量

$original_datetime = new DateTime($_item['updated_at']); 
$expire_time = $original_datetime->modify("+72 hours"); 

HTML:與其10000即10秒

<input type="button" id="acceptBtn" value="<?php echo $this->__('Accept') ?>" 
data-created-at="<?php echo $original_datetime->format("d-M-Y H:i:s"); ?>" 
data-expiring-at="<?php echo $expire_time->format("d-M-Y H:i:s"); ?>" ><br> 
<span id="expired_message"></span> 

jQuery的

jQuery(document).ready(function ($) { 
    disableAcceptButton(); 
} 
function disableAcceptButton() { 
    setTimeout(function() { 
     jQuery("#acceptBtn").prop("disabled", true).hide(); 
     jQuery("#expired_message").text('Sorry, time expired, if you wish to buy/sell this Product again, Contact Admin via Email'); 
    }, 10000); 
} 

,這應該從創建起3天后過期。

幫助。

+2

的jQuery不會幫助,因爲用戶無法打開一個頁面3天...你必須節省分貝 – Babar

+1

之日起計算「結束」日期並將其存儲在一個數據庫,那麼當EV呃調用頁面/按鈕,檢查按鈕是否仍然有效。 – Epodax

+0

你能告訴我$ expire_time var格式嗎? –

回答

1

其實,你將不能夠使用jQuery的setTimeout做到這一點()函數,你需要的東西更穩定比

你可以用JS腳本塊內PHP這樣做:

<script> 
<?php 
$current_time = time(); 
//connect to db 
// check if there is no expire timestamp in db 
if($thereIsNoTimestampInDB){ 
    $expire_time = $current_time + (24 * 60 * 60 * 3); // increase 3 days to timestamp 
//save timestamp to database 
}else{ 
//get Timestamp from DB and save it to $expire_time 
if($expire_time > $current_time){ 
echo "var expired=true;\n"; 
}else{ 
echo "var expired=false;\n"; 
} 
} 
?> 
</script> 

然後與JS:

<script> 
if(expired){ 
$("#Button").html('Sorry Button Expired, Please Contact Admin'); 
} 
</script> 
+0

如果您發現我的回答是正確的,請將其標記爲此問題的正確答案 –

相關問題