Insert in the value of an input type="date" the date returned from the Select of the database

Asked

Viewed 1,397 times

0

I’m taking a date from the database and displaying it in a field input in HTML.

Only, for the client to be able to edit as a date, I need it to be in an input type="date". However, there seems to be some restriction in HTML5 on this type of input.

How could I insert this data into the input I want?

Example of my code:

 <input type="date" class="form-control" name="" id=""  value="<?=$data;?>" disabled >
  • How’s the date in the bank?

2 answers

2


Assuming the seat date is in the format YYYY-MM-DD hh:mm:ss

 //data retornada do banco
 $dataTime="2018-05-08 20:25:22";

 //parte válida da data para inserir no input type="date" YYYY-MM-DD
 $data = substr($dataTime,0,10); //2018-05-08

 <input type="date" class="form-control" name="" id="" value="<?=$data;?>" disabled >

to be able to edit you have to take this disabled

  • The substr returns a piece of the string. To do this it uses three parameters: the string itself, the initial index and the number of characters to be returned from the initial index.

Expected result:

 <input type="date" class="form-control" name="" id="" value="2018-05-08">

  • Perfect! Before I was converting the format, because in HTML it displays dd/mm/yyyy. And I just picked up the date "2018-05-08", making it even easier. The biggest move was not knowing I had to take the 'Disabled'. Vlw comrade!

1

Date has to be in format YYYY-MM-DD.

Ex:

<input type="date" name="data" value="2013-12-01">
  • Thanks for the collaboration, buddy! However, I will mark the @leo solution, as it is more complete and also deal with PHP and the input constraint.

Browser other questions tagged

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