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条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab