PHP - 数据库中的时间值和选择菜单匹配

时间:2015-09-22 00:29:57

标签: php

我正在开发一个HTML表单,其中包含一个选择菜单,该菜单由从数据库中检索的PHP数组驱动,以显示可为事件选择的时间列表。这是阵列:

Array ( [9:00am] => 9:00am [9:30am] => 9:30am [9:45am] => 9:45am [10:00am] => 10:00am [10:30am] => 10:30am [10:45am] => 10:45am [11:00am] => 11:00am [11:30am] => 11:30am [11:45am] => 11:45am [12:00pm] => 12:00pm [12:30pm] => 12:30pm [12:45pm] => 12:45pm [1:00pm] => 1:00pm [1:30pm] => 1:30pm [1:45pm] => 1:45pm [2:00pm] => 2:00pm [2:30pm] => 2:30pm [2:45pm] => 2:45pm [3:00pm] => 3:00pm [3:30pm] => 3:30pm [3:45pm] => 3:45pm [4:00pm] => 4:00pm [4:30pm] => 4:30pm [4:45pm] => 4:45pm [5:00pm] => 5:00pm [5:30pm] => 5:30pm [5:45pm] => 5:45pm [6:00pm] => 6:00pm [-] => - )

问题是数据库中相应字段中的现有值未显示为已选中,因为它们与数组/选择菜单中的某个选项不匹配。例如,当您通过PHP从数据库中检索事件记录并显示事件时间字段的值时,它显示为“11:45:00”,这与“选择”菜单中的任何选项都不匹配,因此不会t显示为“已选中”。

我不确定如何以格式轻松转换时间:

11:45:00

到与选择菜单匹配的格式的相应时间:

上午11时45分

1 个答案:

答案 0 :(得分:1)

你应该试试这个:

$d = DateTime::createFromFormat('H:i:s', '11:45:00'); //the second parameter is the one you should change
echo $d->format('H:iA'); //This echo in the format you want;

我不知道您获取值的方式或位置,但更改第二个参数。您可以在此处阅读更多内容DateTime::createFromFormat