Display Mysql date in date field

Asked

Viewed 1,192 times

2

I have a customer registration page with a field for date of birth. I can choose a date and save it in the database. But when I return to the registration screen of this client this field (type="date") is not displaying the value saved in the database. Can anyone help me understand what is wrong with my code ?

<? $dtnascimento = '';
if($rst[0]['datanascimento'] != '') {
   $dtnascimento = $rst[0]['datanascimento'];
   $dtnascimento = date_format(date_create($dtnascimento),"d/m/Y");
} ?>

<label>Data de Nascimento</label>
<input type="date" id="datansc" name="datansc" value="<? echo $dtnascimento ;?>"/>

  • When you give the $dtnascimento die it returns the date as?

  • the customer’s date of birth in the format I chose "d/m/Y" 15/02/1975

2 answers

3


Try to format the date to YYYY-MM-DD.

This is the format accepted by HTML5.

If you really need to modify or mask the date try to view this topic: https://stackoverflow.com/questions/6978631/how-to-set-date-format-in-html-date-input-tag

<div>
Note que ele despreza a data:
<input type="date" id="datansc" name="datansc" value="15/02/1975"/>
</div>

<div>
Note que agora ele exibe o valor:
<input type="date" id="datansc" name="datansc" value="1975-02-15"/>
</div>

0

What you need to use is this:

<?= date_format(new DateTime($dtnascimento),'d/m/Y') ?>

and it only works with date type if I’m not mistaken, if you’re datetime in the database or in your html, you have to edit, but it’s not cool, like:

<?= date_format(new DateTime($dtnascimento),'d/m/Y H:i:s') ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.