1

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 } ?>
1
  • type="time" inputs have to use a valid RFC3339 timev alue. you're not providing that, so you get the default --- stuff. try date('H:i:s') instead. am/pm are not valid in this context. Commented Sep 29, 2015 at 16:48

3 Answers 3

9

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" />
Sign up to request clarification or add additional context in comments.

3 Comments

its displaying with 1-12 but when its 13-24 its displaying as AM always AM
Need to change h to H -> date("H:i", strtotime($row['time_d']));
really thanks man i'm stuck with this input type time for almost 1 hour :D
0

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

echo "$date";

Replace it with

echo $date;

3 Comments

Provide the value of $row['time_d'] variable.
"$foo" and $foo are identical as far as PHP is concerned. this is not the problem.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.