Displaying dates in forms

Asked

Viewed 148 times

0

I am creating a form that contains a field whose data type is DATE, where I want a certain value returned by default. But there’s 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>
  • good field type date gets a specific type of formatting that I can’t remember now to pass you

1 answer

2


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

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

Then just change the function call date in PHP:

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

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

Browser other questions tagged

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