html的input(日期date)类型的value赋值,必须是"2022-04-25"这样的,月和日必须是2位才可以。
<input type="date" name="acceptdate" value ="2022-04-25"/>
如果从数据库取到的是"2022-4-3"是无法显示的。而且console会有提示:
The specified value "2022-3-5" does not conform to the required format, "yyyy-MM-dd".
使用php的date()和strtotime()函数,配合一下就好了。
$a = "2022-4-3";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>"; //2022-04-03
下面是对date()和strtotime()函数的进一步的随意测试:
<?php
$a = "你好字符";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>"; //1970-01-01
$a = "1";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//1970-01-01
$a = "2";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//1970-01-01
$a = "100";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//1970-01-01
$a = "1900";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//2022-04-23
$a = "1959";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//2022-04-23
$a = "1960";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//1960-04-23
$a = "1969";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//1969-04-23
$a = "2022";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//2022-04-23
$a = "2023";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//2022-04-23
$a = "20223";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//1970-01-01
$a = "2022-3";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//2022-03-01
$a = "2022-3-5";
$ab = date("Y-m-d",strtotime($a));
echo "$a 返回的日期:$ab <br>";//2022-03-05
$a = "2022-03-05";
$ab = date("Y-m-d",strtotime($a));
echo "$a 这个带0返回的日期:$ab <br>";//2022-03-05
?>