默认情况下如何设置HTML日期时间输入值

时间:2022-10-22 17:18:08

For example, I have a textbox in which I want to set by default a value from database. I can do it like this:

例如,我有一个文本框,我想在其中默认设置数据库中的值。我可以这样做:

<input type="text" name="abcd" value="<?=@$result['xyz'];?>"/>

Ok, it works for this type of field.

好的,它适用于这种类型的领域。

But, I want to do it for a date-time field like this:

但是,我想在这样的日期时间字段中执行此操作:

$datetime = $result['datetime']; 
//already converted to format("d/m/Y, h:i:s A")
<input type="datetime-local" value="<?=@$datetime?>" name="abc"/>

I've already tried converting the database date-time format to fit this format. No result.

我已经尝试过转换数据库日期时间格式以适应这种格式。没有结果。

Ideas? Thanks!

1 个解决方案

#1


Try using:

$datetime = new DateTime($result['datetime']);

<input type="datetime-local" value="<?= $datetime->format('Y-m-d H:i:s'); ?>" name="abc" />

This will always try to format your date and time to a correct format. If this fails, PHP will throw an exception letting you know what's wrong.

这将始终尝试将您的日期和时间格式化为正确的格式。如果失败,PHP将抛出一个异常,让你知道什么是错的。

#1


Try using:

$datetime = new DateTime($result['datetime']);

<input type="datetime-local" value="<?= $datetime->format('Y-m-d H:i:s'); ?>" name="abc" />

This will always try to format your date and time to a correct format. If this fails, PHP will throw an exception letting you know what's wrong.

这将始终尝试将您的日期和时间格式化为正确的格式。如果失败,PHP将抛出一个异常,让你知道什么是错的。