dongyong1942 2017-06-28 08:08
浏览 41
已采纳

如何在PHP中剩余时间结束后隐藏或禁用文本框

I am building an auction website in php. I have calculated remaining time till an auction is live. The remaining time is calculated as $remaning = $date - time(); where $date is the last date till auction of a product is live and it is fetched from the database using mysqli as shown below

<?php
if ($stmts = $mysqli->prepare('SELECT end_date FROM products WHERE pid = ?')) {
    $stmts->bind_param("i",$pid); 
    $stmts->execute(); // Execute the prepared query.
    $stmts->bind_result($endDate); // get variables from result.
    $stmts->fetch();
    $stmts->close();

    $date = strtotime($endDate);
    $remaning = $date - time();
}
?>

I am accepting bidding amount through a text box in my page as shown below.

<form ...>  <!-- Form to submit bidding amount starts here -->
    <input type="text" placeholder = "Bid Price" name="bid_price" id="bid_price" title="Enter your Bid for auction" required />
    <input type="submit" class="sbmt"  name="bidsubmit" id="bidsubmit" value="Submit"/>
</form>     <!-- Form ends here -->

How can I make that text box having id bid_price disabled or hide it after remaining time is over, so that no user can bid after that.

  • 写回答

2条回答 默认 最新

  • drhqkz3455 2017-06-28 08:17
    关注

    With what you've provided I can simply suggest using a condition as to whether the date is after now like so:

    $date = DateTime('now');
    $date->modify('+ 2 day');//testing negative
    
    if ($date > DateTime('now')) {
        //show form
    } else {
        //auction has ended
    }
    

    In the snippet above if you set the modify function to - 1 days the form will not show! Yeyyyyys

    Note that I've set the operator to > rather than >= so that the form does not show exactly when the action is set to end. Another thing to bear in mind is timezones! But this shouldn't matter too much in your question?

    Furthermore

    Being a lover of packages in the packagist sense there is a package called Carbon which I think is really cool and simple to use.

    if ($date->lt(Carbon::now()) {
    }
    

    It also has some cool features for date formatting and interval formatting. In this scenario I'd recommend using the diffForHumans method which gives some cool output like 2 days, 30 seconds ago. There's probably a method or parameter you can set to say Ends in 30 seconds time

    http://carbon.nesbot.com/docs/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?