dongshan8953 2011-03-22 01:13
浏览 63
已采纳

如何从<select>输入时间到mysql并输出am / pm?

Hey guys so, im doing this form with a input that will contain the time and i want to make it so that when users choose the time (for example 8:30 AM), it will be stored in mysql, and will be output by php with the exact time (8:30 AM).

Here is the form. A simple select button with increments of 30 on the time.

http://jsfiddle.net/jXVPS/

The main problem is i dont know how mysql will recognize if it is AM/PM?

What values i should put for the select fields, how i would insert it into the mysql database, and have mysql or php recognize wheather it is 1:00 AM or 1:00 PM. Is there some sort of 24 hour clock in mysql that makes it recognize wheather or not it is AM/PM?

  • 写回答

3条回答 默认 最新

  • doucai7294 2011-03-22 01:26
    关注

    Here's MySQL's time/date stuff. I think the TIME type would be best for you since you don't need a date, and the format you've got for your values should fit well. I think you can just put them as strings exactly like that.

    http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
    http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html

    After you submit thru form submission or ajax and your responding PHP page takes over, perform a query with the following string in the PHP:
    "INSERT INTO table (userId, time, ...) VALUES (..., '$inTime', ...)"

    And then echo it out since you want the next page to repeat it.


    Response to comment

    I figure you mean that wou want 13:00:00 to be 1:00 PM when you echo it out later right?

    Theres probably something you can do with time functions for either PHP or MySQL (PHP below, MySQL above), but I don't know from memory and this is simple enough you can just do it on your own. http://www.php.net/manual/en/ref.datetime.php

    $time = //get time field from MySQL
    list($h, $m, $s) = explode($time, ':');
    if ($h > 12) {$h -= 12; $amOrPm = 'PM'}; else {$amOrPm = 'AM';}
    if ($h == 0) {$h = 12;}
    
    echo "$h:$m $amOrPm";
    

    PS - 13:00:00 is 1pm, not am.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?