使用PHP更改日期输入的值

使用PHP更改日期输入的值,php,mysql,date,Php,Mysql,Date,我有以下意见: <input type="date" name="datin" value=""> 我需要将此输入的值更改为$row['eventoDatIn']的值,这也是格式为YYYY-MM-DD的日期 我试着用三种方式来回应这个值: <input type="date" name="datin" value=" <?php $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) >

我有以下意见:

<input type="date" name="datin" value="">

我需要将此输入的值更改为$row['eventoDatIn']的值,这也是格式为YYYY-MM-DD的日期

我试着用三种方式来回应这个值:

<input type="date" name="datin" value="
<?php
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) { 
echo date($row['eventoDatIn']); 
}
}
?>">

的第一个参数实际上是日期格式。格式无效的字符按原样打印

因此,基本上你是在以一种不包含任何有效格式选项的格式回显当前时间,因此结果是“格式”(实际上是你的日期)被逐字输出

试一试

PS:你说“我有一个这种格式的日期”,但我希望这个日期实际上是数据库中的真实日期(时间戳)。如果它是一个格式化字符串,您可能必须首先将其转换为时间戳。如果可能,在数据库中这样做,并将日期和时间存储为实际日期和时间,而不是字符串

在MySQL中,您可以通过PHP读取:

SELECT UNIX_TIMESTAMP(YourDateField) AS TheDate FROM YourTable

不,问题出在编码中,您错过了
日期
函数的第一个参数,即
格式说明符
,第二个参数也是第二个
,请尝试以下代码:-

<input type="date" name="datin" value="
<?php
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) { 
echo date('Y-m-d', strtotime($row['eventoDatIn'])); // check the change 
}
}
?>">
不起作用:(但它可能是解决方案的一部分,$row['eventoDatIn']中的这个值作为日期存储在数据库中,看起来是这样的:2015-05-23。使用查询中的函数将日期作为时间戳返回,PHP可以使用它。
echo date('Y-M-d', $row['eventoDatIn']); 
SELECT UNIX_TIMESTAMP(YourDateField) AS TheDate FROM YourTable
<input type="date" name="datin" value="
<?php
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) { 
echo date('Y-m-d', strtotime($row['eventoDatIn'])); // check the change 
}
}
?>">