Displaying dates on forms

0

I'm creating a form that contains a field whose data type is DATE, where I want a given value to be returned by default. But there is nothing I can do to make it happen. My code is this:

<?php $ini = date('d-m-Y'); ?>
<form id="form1" name="form1" method="post" action="">
  <label for="inicio" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#666666;">
  De</label> <input type="date" name="date_form" id="inicio" size="7" value="<?php echo $ini; ?>" /> 
   a
  <label for="label">Fim</label>
  <input type="text" name="textfield2" id="label" />
</form>
    
asked by anonymous 13.09.2017 / 13:07

1 answer

2

The problem is in formatting the date. In order for the field to recognize the value you want to display, it must be in YYYY-mm-dd format. See:

Errado: <input type="date" value="13-09-2017">
Certo: <input type="date" value="2017-09-13">

So just change the call of the date function in PHP:

$ini = date("Y-m-d");
  

Remembering that not all browsers can have the same behavior for a type="date" field, as shown on CanIUse .

    
13.09.2017 / 14:19