用PHP更改日期输入的值

时间:2015-06-04 20:38:41

标签: php mysql date

我有这个输入:

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

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

我尝试用3种方式回应价值:

<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']); 
}
}
?>">

此:

<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(htmlspecialchars ($row['eventoDatIn'])); 
}
}
?>">

而且:

<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 (htmlspecialchars ($row['eventoDatIn']));  
}
}
?>">

输入始终只显示默认的mm / dd / yyyy值。 这是日期格式的问题吗?

2 个答案:

答案 0 :(得分:2)

问题出在您的编码中,您错过了date函数的第一个参数format specifier,第二个参数是value in second,请尝试以下代码: -

<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 
}
}
?>">

答案 1 :(得分:0)

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

所以基本上你以一种不包含任何有效格式选项的格式回显当前时间,所以结果是&#39;格式&#39; (实际上是你的日期)按字面输出。

尝试

echo date('Y-M-d', $row['eventoDatIn']); 

PS:你说我有这种格式的日期&#39;,但我希望日期实际上是数据库中的实际日期(时间戳)。如果是格式化字符串,则可能必须先将其转换为时间戳。如果可能,请在数据库中执行此操作,并将日期和时间存储为实际日期和时间,而不是字符串。

在MySQL中你可以convert dates to UNIX_TIMESTAMPS,这可以被PHP读取:

SELECT UNIX_TIMESTAMP(YourDateField) AS TheDate FROM YourTable