Set current date in PHP and put HTML value in an input type='date'

Asked

Viewed 3,033 times

0

I tried it this way but it doesn’t work

 <form action="add.php" method="POST" name="form1">
      <div class="w3-section">
        <label>Nome do funcionario</label>
        <input class="w3-input w3-border" placeholder="Nome" type="text" name="name" required>
      </div>
      <div class="w3-section">
        <label>Horario</label>
        <input class="w3-input w3-border" placeholder="Hora" type="text" name="hora" required>
      </div>
      <div class="w3-section">
        <label>Data</label>
        <input class="w3-input w3-border" value="<?php echo date('d/m/Y');?>" type="date"  name="data" required>
      </div>
      <button type="submit" class="w3-button w3-padding-large w3-green w3-margin-bottom">Adicionar <i class="fa fa-plus"></i></button>
    </form>  

he ignores the value part

2 answers

0


Try it this way:

<form action="add.php" method="POST" name="form1">
    <div class="w3-section">
        <label>Nome do funcionario</label>
        <input class="w3-input w3-border" placeholder="Nome" type="text" name="name" required>
    </div>
    <div class="w3-section">
        <label>Horario</label>
        <input class="w3-input w3-border" placeholder="Hora" type="text" name="hora" required>
    </div>
    <div class="w3-section">
        <label>Data</label>
        <input class="w3-input w3-border" value="<?php echo date('Y-m-d');?>" type="date"  name="data" required>
    </div>
    <button type="submit" class="w3-button w3-padding-large w3-green w3-margin-bottom">Adicionar <i class="fa fa-plus"></i></button>
</form>

Replacing date('d/m/Y') for date('Y-m-d'), as this is the appropriate format for the input type='date'

NOTE: if you modify what I indicated in the snippet will not work because here in the stack there is no php snippet

  • Thank you so much, but when it sends to the bank send 2018-04-12 know how I can solve this ?

  • You can make the conversion using strtotime and date as follows: $data_wrong = "2018-04-12"; $data_correct = date("d/m/Y", strtotime($data_wrong));

  • Thank you very much, helped a lot and worked right.

0

The value attribute format of the type field date should be in format yyyy-mm-dd or the php format (date('Y-m-d')), the format displayed will depend on the browser location setting. Note that not all browsers support this type of field.

 <form action="add.php" method="POST" name="form1">
      <div class="w3-section">
        <label>Nome do funcionario</label>
        <input class="w3-input w3-border" placeholder="Nome" type="text" name="name" required>
      </div>
      <div class="w3-section">
        <label>Horario</label>
        <input class="w3-input w3-border" placeholder="Hora" type="text" name="hora" required>
      </div>
      <div class="w3-section">
        <label>Data</label>
        <input class="w3-input w3-border" value="<?php echo date('Y-m-d');?>" type="date"  name="data" required>
      </div>
      <button type="submit" class="w3-button w3-padding-large w3-green w3-margin-bottom">Adicionar <i class="fa fa-plus"></i></button>
    </form>  

  • Thank you so much, but when it sends to the bank send 2018-04-12 know how I can solve this ?

Browser other questions tagged

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