PHP - 数据库中的时间值和选择菜单匹配
I'm working on a HTML form that has a select menu that's driven from a PHP Array that is retrieved from a database to show a list of times that can be chosen for an event. Here's the array:
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 [-] => - )
The problem is that existing values in the corresponding field in the database are not showing as selected as they don't match one of the options in the array/select menu. For example when you retrieve an event record from the database via PHP and display the value for the event time field it appears as "11:45:00", which doesn't match any of the options in the Select menu so this doesn't show as "selected".
I'm not sure how I can easily convert a time in the format:
11:45:00
to the corresponding time in the format that matches the select menu:
11:45am
我正在处理一个HTML表单,它有一个选择菜单,该菜单是从一个从一个PHP数组中检索的 数据库,显示可为事件选择的时间列表。 这是数组: p>
数组([9:00 am] => 9:00 am [9:30 am] => 9:30 am [9:45 am] => 上午9:45 [上午10:00] =>上午10:00 [上午10:30] =>上午10:30 [上午10:45] =>上午10:45 [上午11:00] =>上午11:00 [11: 30am] => 11:30 am [11:45 am] => 11:45 am [12:00 pm] => 12:00 pm [12:30 pm] => 12:30 pm [12:45 pm] => 12 :下午45点[下午1点] =>下午1点[下午1:30] =>下午1:30 [下午1点45分] =>下午1点45分[下午2点] =>下午2点[下午2点30分] ] => 2:30 pm [2:45 pm] => 2:45 pm [3:00 pm] => 3:00 pm [3:30 pm] => 3:30 pm [3:45 pm] => 3: 下午45点[下午4点] =>下午4点[下午4点30分] =>下午4点30分[下午4点45分] =>下午4点45分[下午5点] =>下午5点[下午5点30分] => 5:30 pm [5:45 pm] => 5:45 pm [6:00 pm] => 6:00 pm [ - ] => - )
code> pre>
问题是数据库中相应字段中的现有值未显示为已选中,因为它们与数组/选择菜单中的某个选项不匹配。 例如,当您通过PHP从数据库中检索事件记录并显示事件时间字段的值时,它显示为“11:45:00”,这与“选择”菜单中的任何选项都不匹配,因此不会 t显示为“已选择”。 p>
我不确定如何以格式轻松转换时间: p>
11: 45:00
code> pre>
以与选择菜单匹配的格式显示相应的时间: p>
上午11:45 p >
div>
You should try this:
$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;
I don't know how or where you get the values, but change the second parameter. You can read more here DateTime::createFromFormat