如何格式化从 mysql 获取的 php 中的值 "datetime -local"?

How to format "datetime -local" value in php fetched from mysql?

我在 html 中有一个输入字段,我想在其中回显存储在 mysql 中的日期时间值。

<input type="datetime -local" name="schedule" value="<?php echo $result['schedule']; ?> 

但是,尽管 mysql db 中存在值,因为 YY/mm/dd h:m:s value 没有显示。可能的问题是什么?当我尝试在输入值之外回显时,它会显示结果。我的想法是日期时间需要根据 html datetime -local input field.

进行格式化

如果可能的话,请给我一些建议。

你填错了。阅读 documentation about the input-type datetime-local 正确的格式是 YYYY-MM-DDTHH:MM - 注意 T 作为日期和时间的分隔符。

还有:

  1. 你写错了类型。正确的是 datetime-local
  2. 您在 value 属性中缺少结束引号。

T是格式字符(时区缩写e.g: EST, MDT...)所以不能直接使用。将其转义 (\T) 以获得文字 T 字符:

datetime.format.php

You can prevent a recognized character in the format string from being expanded by escaping it with a preceding backslash.

<?php
    $date = date("Y-m-d\TH:i:s", strtotime($result['schedule']));
?>
<input type="datetime-local" name="schedule" value="<?php echo $date; ?>"/> 

使用<?=$date?>代替<?php echo $date; ?>

这是解决方法

<input type="datetime-local" name="schedule" value="<?php echo $date = date("Y-m-d\TH:i:s", strtotime($result['schedule']));?>">