Frank Lucas Frank Lucas - 11 months ago 47
PHP Question

HTML/PHP display current date in html date input

I am using a

date input
type to select a date in my PHP project.

Now everytime I submit the form I want to display the
in the input that has been selected.

This is what I've tried:

<?php $date = $_GET['date']; ?>
<input placeholder="<?php $date ?>" type="date" name="date" id="date" />

But unfortunately this doesn't seem to work.

Anyone has any idea if my goal is possible?

Many thanks in advance!


You can't set a placeholder to <input type="date"/>. See the following information from HTML specification:

The following content attributes must not be specified and do not apply to the element: accept, alt, checked, dirname, formaction, formenctype, formmethod, formnovalidate, formtarget, height, inputmode, maxlength, minlength, multiple, pattern, placeholder, size, src, and width.

You have to set the date as value like the following:

<?php $date = $_GET['date']; ?>
<input type="date" name="date" id="date" value="<?= $date ?>" />

An example code:

<input placeholder="2011-01-01" type="date" id="date"/>
<input value="2011-01-01" type="date" id="date"/>