dtpfia3334 2015-09-22 00:29
浏览 10
已采纳

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

  • 写回答

1条回答 默认 最新

  • dongshie8450 2015-09-22 02:23
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?