input datetime-local value

0

Hello friends, how are you?

In my database I have the date time like this: 2018-06-22 12:00:00

in the final html looks like this:

<input type="datetime-local" class="form-control" id="data_contratado" name="data_contratado" value="2018-06-22 12:00:00">

But in the end I can not display the value in the field.

Doing me a favor could you help me?

    
asked by anonymous 22.06.2018 / 20:58

3 answers

2
  

To cover any and all date from the bank I would do it this way:

$dataInput =  $date->format('Y-m-d\TH:i:s');
  

that would meet the following situations

//data completa
$date = new DateTime('2018-06-22 12:00:00');
$dataInput =  $date->format('Y-m-d\TH:i:s');                 
echo '<input type="datetime-local" value="'.$dataInput.'">';
//resultado 22/06/2018 12:00

//data incompleta
$date2 = new DateTime('2018-06-22');
$dataInput2 =  $date2->format('Y-m-d\TH:i:s');               
echo '<input type="datetime-local" value="'.$dataInput2.'">';
//resultado 22/06/2018 00:00
  

Notice that in the second case there is no space after the date day, thus invalidating your solution str_replace(' ','T','2018-06-22') which will return dd/mm/aaa --/- -

To test these codes online PHPTester

    
22.06.2018 / 22:32
1

I was able to resolve it as follows:

str_replace(' ','T','2018-06-22 12:00:00')

But I do not know if it's the best way

    
22.06.2018 / 21:01
1

You can use this way: $dateHtml = date("Y-m-d\TH:i:s", strtotime($date)); and add value in html.

    
22.06.2018 / 21:18