如何插入<;输入类型=“;时间“/>;进入数据库


How to insert the value entered in <input type="time"/> into database?

我有一个html代码,其中包含一个。我在输入字段中输入了一个类似"01:00 AM"的值。当我将输入的时间值插入数据库表时,只有"01:00"被插入,"am"没有被插入数据库。数据库表中的时间字段使用了数据类型"varchar(20)"。有人能告诉我如何将AM和PM的全职时间插入数据库吗?

       html:
              <form action="projectinser.php" method="post">
                   <div id="dynamicInput" style="display:inline" >
                <input type="text" name="itemname"  pattern="[a-zA-Z ]+" placeholder="Item" value="" size="35">
                    </div>
                    <div id="dynamicInput1" style="display:inline">
                      <input type="text" name="sdate" id="datepicker" placeholder="Enter Start Date" value="" size="34">
                     </div>
                    <div id="dynamicInput" style="display:inline">
                 <input type="time" name="stime" placeholder="Enter Time"  
             value="" size="34">
              </div>
         <div id="dynamicInput2" style="display:inline">
             <input type="text" name="details"  pattern="[a-zA-Z ]+" placeholder="Enter Details" value="" size="34">
             </div>
        </form>
           projectinser.php
            <?php
           $itemname = $_POST['itemname'];
            $sdate    = $_POST['sdate']; 
            $details  = $_POST['details'];
            $stime = $_POST['stime'];
           $sql= "INSERT INTO projectdetail (projectid, item, remainderdate, time, details) VALUES ('$cid', '$itemname', '$sdate', '$stime', '$details')";
            mysqli_query($con,$sql);
           ?>

将类型更改为type="text"应该可以解决此问题,因为您正在为表使用varchar。

<input type="text" name="stime" placeholder="Enter Time"   

您可以做两件事。您可以将列更改为实际的DATETIME或TIME字段。这样,它将以实时格式存储时间。

或者您可以在插入时执行此操作。

$input = $_POST['input'];
$the_time = date('h:i A', strtotime($input));

我在这里提供了一个示例,其中有3个示例,时间戳被转换为您想要的格式。

示例然而,我建议将它们存储为实际的时间戳,这样你就可以在未来根据需要对它们进行操作

更新:(包括您的代码)

 $stime = $_POST['stime'];
 $the_time = date('h:i A', strtotime($stime));
 $sql= "INSERT INTO projectdetail (projectid, item, remainderdate, time, details) VALUES ('$cid', '$itemname', '$sdate', '$the_time', '$details')";
        mysqli_query($con,$sql);