The HTML <input type="date">
element expects the value
attribute to be set in "YYYY-MM-DD
" format. Therefore, if you're setting a default value to it using PHP, then you must adhere to the correct date format (i.e. "Y-m-d
" in PHP).
For example, you can set the current date using DateTime
(or DateTimeImmutable
), in the following way:
$dt = new DateTime(); echo '<input type="date" value="' . $dt->format('Y-m-d') . '" />';
Similarly, you can set a specific date using DateTime
(or DateTimeImmutable
), in the following way:
$dt = new DateTime('16.10.2022'); echo '<input type="date" value="' . $dt->format('Y-m-d') . '" />';
There are several other ways in which you can create a formatted date in PHP. For example, you may use the date_create()
function as an alternative, which creates a new DateTime
object:
$dt = date_create('16.10.2022'); echo '<input type="date" value="' . $dt->format('Y-m-d') . '" />';
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.