使用PHP的HTML输入类型时间设置值


HTML Input Type Time Set Value using PHP

我试图用AM/PM在HTML输入类型Time中显示Time,但我的代码似乎没有显示在输入类型中。

这是我的HTML输入时间输出。

9:00 AM

但我的HTML输入类型时间仍然是

--:-- --

这是我的代码

<?php
                                    //set up mysql connection
                                    mysql_connect("localhost", "root", "") or die(mysql_error());
                                    //select database
                                    mysql_select_db("db") or die(mysql_error());
                                    //select all records form tblmember table
                                    $query = 'SELECT * FROM newsfeed';
                                    //execute the query using mysql_query
                                    $result = mysql_query($query);
                                   //then using while loop, it will display all the records inside the table
                                    while ($row = mysql_fetch_array($result)) { ?>
      <div class="modal fade" id="myModal<?php echo $row['id'] ?>" role="dialog">
          <div class="modal-dialog">
              <!-- Modal content-->
              <div class="modal-content">
                  <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal">&times;</button>
                      <h4 class="modal-title">Admin</h4>
                  </div>
                  <div class="modal-body">
                      <form id="updateform" action="admin_update.php" method="post">
                          <fieldset>
                            <!-- image-preview-filename input [CUT FROM HERE]-->
                              <div class="form-group">
                                  <img src="<?php echo $row['image'] ?>" class="img-responsive" />
                              </div>
                              <div class="form-group">
                                  <label>When</label>
                                  <br/>
                                  <input type="time" class="form-control" value="<?php $date = date("h:i A", strtotime($row['time_d'])); echo "$date"; ?>" id="until_t" name="until_t" />
                                  <br/>
                              </div>
                      <div class="modal-footer">
                          <input type="submit" id="btnsave" name="btnsave" class="btn btn-default" value="Save" />
                          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                      </div>
                  </div>
                  </form>
          </div>
      </div>
          </div>
      </div>
            <?php } ?>

<input type="time" />value需要为24小时格式,不包含AM/PM。看见http://www.w3.org/TR/html-markup/input.time.html

因此,尝试在date()中不使用A,并使用H代替h

<input type="time" class="form-control" value="<?php $date = date("H:i", strtotime($row['time_d'])); echo "$date"; ?>" id="until_t" name="until_t" />

您已经用双引号编写了变量。因此,代码中不包含此语句。

echo "$date";

更换为

echo $date;

有两种方法可以实现这一点:Mysql方法和php方式。

Mysql:

SELECT DATE_FORMAT(time_column_name, '%H:%i') FROM table_name // this will return the time the same format as the input of type time sends 

Php:

$formated_time = date("H:i", strtotime($row['time_column_name'])); // this will convert the time to 24 hours also the same the input of type time sends