input datetime-local value

Asked

Viewed 4,012 times

1

Hello friends all right ?

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

in the final html is this way:

<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’t show value in the field.

Do me a favor and help me ?

3 answers

6


To cover any and all dates coming from the bank I would do so:

$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

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

To test these codes online Phptester

3

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

2

I managed to solve as follows:

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

But I don’t know if it’s the best way

  • I liked the solution!!

  • and if the date were 2018-06-22

  • What a wonderful way to solve the problem. :)

Browser other questions tagged

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