我使用<input type="datetime-local">
数据库现在我要回去了更新目的<input type="datetime-local">
日期存储日期。
我在做
$query="select * from exam_schedule where subject_name='$name' AND class_id='$schedule_id' limit 1";
$result= mysqli_query($connection,$query);
while ($read_all_data = mysqli_fetch_assoc($result))
{
echo "date comming from datebase::".$date=$read_all_data['date_and_time']."<br>";
echo "duration comming from datebase::".$duration=$read_all_data['duration'];
$duration=$read_all_data['duration'];
echo "<form method='post' action='edit_schedule.php'>";
echo "<input type='hidden' name='id' value='$id'>";
echo "<tr><th><input type='datetime-local' name='date' value='$date'> </th>";
echo "<th><input type='datetime-local' name='duration' value='$duration'> </th>";
echo <input type='submit' name='update' value='update'> </form></th></tr>";
}
当我回声$日期和时间$它为我的值,但是,当我把值=“$日期”这亘古不变的告诉我,从数据库的日期。
问题是,datetime-local
输入型预计日期为这种格式:yyyy-mm-ddThh:mm
但你提供它由是DATETIME
数据库创建的默认yyyy-mm-dd hh:mm:ss
格式。
为了改变它,你需要在你的SQL查询改成这样:
SELECT *, DATE_FORMAT(date_and_time, '%Y-%m-%dT%H:%i') AS custom_date
FROM exam_schedule
WHERE subject_name='$name'
AND class_id='$schedule_id'
LMIT 1
所以现在在你的结果,你就可以使用custom_date
预填您的输入。
<input type='datetime-local' name='date' value='<?php echo $row['custom_date'] ?>' >
如果您需要的时候转换成PHP,而不是MySQL的:
PHP代码示例:
const HTML_DATETIME_LOCAL = 'Y-m-d\TH:i'; //escape the literal T so it is not expanded to a timezone code
$php_timestamp = strtotime($mysql_date_string);
$html_datetime_string = date(HTML_DATETIME_LOCAL, $php_timestamp);
echo "<input type='datetime-local' name='date' value='{html_datetime_string}'>";
您可以使用此之一:
date_format($date, 'Y-m-d H:i:s');