使用当前日期+ 14天填充HTML5输入字段

时间:2019-06-24 16:08:56

标签: php html date datetime

用户正在提交硬币,当他们这样做时,我希望将日期加载到用于跟踪提交时间的输入字段中。该日期将记录在mySQL数据库中。截止日期设置为下一个双月星期五(例如,截止日期为6月14日和6月28日。如果今天完成提交,则截止日期为6月28日。如果提交于6月30日完成,则截止日期为日期将是7月12日。我将整个表格包括在内,以便您能看到更大的图片。请随时进行调整。非常感谢您的帮助。

我可以调用javascript函数,并让用户从日历中选择日期,但这不是我所需要的。 Javascript - get a date from an HTML input,但我不确定如何将下一个截止日期的+14天串联起来。

CoinSubmission.html

<form action="AdminCoinSub_Code.php" method="POST">
  <h1 id="litheader">Coin Submission</h1>
  <div class="inset">
           <input type="text" list="Store" name="Store" placeholder="Store">
        <datalist id="store">
          <option value="Causeway Bay">
          <option value="Wan Chai">
          <option value="Lai Chi Kok">
          <option value="Tai Po">
        </datalist>    
        <input type="text" list="Position" name="Position" placeholder="Position">
        <datalist id="position">
          <option value="1">
          <option value="2">
          <option value="3">
          <option value="4">
        </datalist>    
        <p>
      <input type="text" name="Nickame" id="Nickname" placeholder="Nickname">
    </p>
    <p>
      <input type="text" name="Contact" id="Contact" placeholder="Contact">
    </p>
    <p>
      <input type="text" name="MachineCount" id="Machine Count" placeholder="Machine Count">
    </p>
    <p>
  <input type="text" name="CutOffDate" id="CutOffDate" placeholder="Cut Off Date">
    </p>
    <p>
      <input type="text" name="Coins" id="Coins" placeholder="Coins">
    </p>
    <p>
        <input type="file" type="text" name="location" accept="image/*">
    <div class="btnConfirm">       
    <input class="loginLoginValue" type="hidden" name="" value="" />
    </div>
  </div>
    <div class="btnConfirm">
        <input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
    </div><br><br>
    <div class="wrapper2">
  <nav>
    <ul>
      <li><a href="AdminSubmission.php" class="active">SUBMISSION</a></li>
      <li><a href="OccupancyListAdmin.php">OCCUPANCY</a></li>
      <li><a href="#">ANALYTICS</a></li>
        <li><a href="#">SEARCH</a></li>
    </ul>
  </nav>
    </div>
    </form>

AdminCoinSub_Code.php

<?php {
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "administrator_logins";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO admincoinsubmission (Store, Position, Nickname, Contact, MachineCount, CutOffDate, Coins, location) 
    VALUES ('$_POST[Store]','$_POST[Position]','$_POST[Nickame]','$_POST[Contact]','$_POST[MachineCount]','$_POST[CutOffDate]','$_POST[Coins]','$_POST[location]')");
    $stmt->bindParam(':Store', $Store);
    $stmt->bindParam(':Position', $Position);
    $stmt->bindParam(':Nickname', $Nickname);
    $stmt->bindParam(':Contact', $Contact);
    $stmt->bindParam(':MachineCount', $MachineCount);
    $stmt->bindParam(':CutOffDate', $CutOffDate);
    $stmt->bindParam(':Coins', $Coins);
    $stmt->bindParam(':location', $location);

    $stmt->execute();



    echo "Success";
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
}
?>

When the page loads the date row in the coin submission form should display the cutoffdate. 

date = (current date + cutoffdate)

2 个答案:

答案 0 :(得分:1)

您可以使用类似的代码完成以下任务-

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
       <meta charset="utf-8">
       <title>Date +14 days</title>
    </head>
    <body>
        <input type="date" name="date1" id="date1" value="" />
        <input type="text" name="date2" id="date2" value="" />

        <script type="text/javascript">
            var date1 = document.getElementById("date1");
            date1.addEventListener('change', function(){
                tempDate = new Date(date1.value);
                finalDate = tempDate.setDate(tempDate.getDate() + 14);
                console.log(new Date(finalDate));
            });
       </script>
   </body>
</html>

答案 1 :(得分:0)

要达到预期效果,请使用以下添加时间选项和 getTime()方法并添加14天

var currentDate = new Date(new Date().getTime()+(14*24*3600000))
document.getElementById('CutOffDate').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear()

工作代码示例以供参考

var currentDate = new Date(new Date().getTime()+(14*24*3600000))
document.getElementById('CutOffDate').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear()
<form action="AdminCoinSub_Code.php" method="POST">
  <h1 id="litheader">Coin Submission</h1>
  <div class="inset">
           <input type="text" list="Store" name="Store" placeholder="Store">
        <datalist id="store">
          <option value="Causeway Bay">
          <option value="Wan Chai">
          <option value="Lai Chi Kok">
          <option value="Tai Po">
        </datalist>    
        <input type="text" list="Position" name="Position" placeholder="Position">
        <datalist id="position">
          <option value="1">
          <option value="2">
          <option value="3">
          <option value="4">
        </datalist>    
        <p>
      <input type="text" name="Nickame" id="Nickname" placeholder="Nickname">
    </p>
    <p>
      <input type="text" name="Contact" id="Contact" placeholder="Contact">
    </p>
    <p>
      <input type="text" name="MachineCount" id="Machine Count" placeholder="Machine Count">
    </p>
    <p>
  <input type="text" name="CutOffDate" id="CutOffDate" placeholder="Cut Off Date">
    </p>
    <p>
      <input type="text" name="Coins" id="Coins" placeholder="Coins">
    </p>
    <p>
        <input type="file" type="text" name="location" accept="image/*">
    <div class="btnConfirm">       
    <input class="loginLoginValue" type="hidden" name="" value="" />
    </div>
  </div>
    <div class="btnConfirm">
        <input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
    </div><br><br>
    <div class="wrapper2">
  <nav>
    <ul>
      <li><a href="AdminSubmission.php" class="active">SUBMISSION</a></li>
      <li><a href="OccupancyListAdmin.php">OCCUPANCY</a></li>
      <li><a href="#">ANALYTICS</a></li>
        <li><a href="#">SEARCH</a></li>
    </ul>
  </nav>
    </div>
    </form>

codepen-https://codepen.io/nagasai/pen/VJWmNE?editors=1010