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条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集