Meyka Jograt
Meyka Jograt

Reputation: 314

HTML Input Type Time Set Value using PHP

I'm trying to display Time in my HTML Input Type Time with AM/PM but it seems that my code is not displaying in the Input Type.

Here's my output for HTML Input Time.

9:00 AM

But my HTML Input Type Time is still

--:-- --

Here's my Code.

<?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 } ?>

Upvotes: 1

Views: 27091

Answers (3)

Ahmed Soliman
Ahmed Soliman

Reputation: 1710

There is two ways to achieve that the Mysql way and the php way .

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

Upvotes: 0

Sean
Sean

Reputation: 12433

The value of <input type="time" /> need to be in 24 Hr format, without the AM/PM. see http://www.w3.org/TR/html-markup/input.time.html

So try without the A in date(), and H instead of 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" />

Upvotes: 9

sandeepsure
sandeepsure

Reputation: 1115

You have written variable in double quotes. So, instead of this statement in you code.

echo "$date";

Replace it with

echo $date;

Upvotes: 0

Related Questions