繁体   English   中英

如何使用PHP和jQuery动态设置按钮的过期时间?

[英]How to dynamically set an expiry-time for a button using PHP and jQuery?

我有一些按钮,说“ Accept并且从创建之时起仅对72 HOURS有效,超过72 72 HOURS ,应该自动禁用,并在<span>一条消息。

PHP:

这些是我收集变量的时间

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

HTML:

<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天而不是10000即10秒。

救命。

实际上,您将无法使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM