fill out datatime field

Asked

Viewed 680 times

1

I’m trying to fill in the input datetime-local but the field is blank, I tried to command the format american and brazilian but it doesn’t come at all. in the database is stored in American format: 2017-02-20 19:19:00 and in html I try to insert like this:

<?php $date = new DateTime($situacao[0]['data_inicio']) ?>
<div class="form-group">
    <label for="txtDataInicio" class="col-sm-2 control-label">Data Inicio</label>
    <div class="col-sm-10">
        <input type="datetime-local" class="form-control" id="txtDataInicio" name="txtDataInicio" placeholder="Nome da Função" value="<?php echo (empty($situacao[0]['data_inicio']))? set_value('txtDataInicio'): $date->format('y-M-dh:m') ?>">
    </div>
</div>

on that line I have tried to format in some formats but without success.

  • Friend, could you put the full code ? It would be easier for us to help you

  • the full code of the display? I will put there

1 answer

1

If only to display from the database in the input, you can display directly, without using the DateTime

<?php $date = (!empty($situacao[0]['data_inicio'])) ? date('d/m/Y H:i:s', strtotime($situacao[0]['data_inicio'])) : set_value('txtDataInicio'); ?>
<div class="form-group">
    <label for="txtDataInicio" class="col-sm-2 control-label">Data Inicio</label>
    <div class="col-sm-10">
        <input type="datetime-local" class="form-control" id="txtDataInicio" name="txtDataInicio" placeholder="Nome da Função" value="<?php echo $date;?>">
    </div>
</div>

In this code it formats to the Brazilian standard dd/mm/YYYY H:i:s but you can use any standard taking into account that is already valid in the function date()

  • not only to be displayed, it will be edited by the user and returned to the database, so I kept the 'type="datetime-local"'

  • In what format it should be in the input for the user to edit ?

  • in the Brazilian format as shown above

  • Just change the type for datetime-local and use the code I passed to you, because it takes the date in the US format of the database and the input it brings formatted in PT-BR for you

  • then, in my code I left in the question, I have tried this way but without success, the input is blank

  • @Danilod checks before if it is actually returning some date by making a print or echo, because according to the code above, it should work normally.

  • It’s @Danilod, do as @Edilson said. Before the code $date of the one print_r($situacao[0]['data_inicio']) to see what is returning.

Show 2 more comments

Browser other questions tagged

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