将javascript日期和时间对象插入数据库

lf5gs5x2  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(395)

我的表单上显示了日期和时间,现在我的问题是如何将日期和时间的值插入数据库,希望您能帮助我。谢谢。
这是我的日期和时间代码:

<div class="row" style="margin-left:300px;">
             <label>Date:</label>  
             <span id="date" name="calendar"></span>
             </div>
              <div class="row" style="margin-left:300px;">
             <label>Time:</label> 
            <span id="clock" name="time"></span>
             </div>

我的php代码:

<?php
include_once ('connection.php');
$calendar=$_POST["calendar"];
$calendar=date("Y-m-d",$calendar);
$time=$_POST["time"];
$time=date("h-i-s",$time);

        $InsertSql = "INSERT INTO test2(date,timein) VALUES ('$calendar','$time')";
          $res = mysqli_query($conn, $InsertSql);

?>

日期和时间的javascript:

<script>
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var n = new Date();
var y = n.getFullYear();
var m = n.getMonth();
var d = n.getDate();
document.getElementById("date").innerHTML = months[m] + " " + d + " " + y;
</script>

<script>
(function () {

  var clockElement = document.getElementById( "clock" );

  function updateClock ( clock ) {
    clock.innerHTML = new Date().toLocaleTimeString();
  }

  setInterval(function () {
      updateClock( clockElement );
  }, 1000);

}());
</script>

更新了我的问题。
这是我的表格
错误

iyzzxitl

iyzzxitl1#

把它存储在一个变量里怎么样。像这样的

<form action="" method="POST">
    <button name="submit" class="submit">Send</button>
</form>
<?php
if(isset($_POST['submit']))
{
$calendar=date("Y-m-d");
$time=date("H:i:s");
$InsertSql = "INSERT INTO test2(date,timein) VALUES ('$calendar','$time')";
          $res = mysqli_query($conn, $InsertSql);
}        

?>

相关问题