drfqfuhej48511519 2011-06-12 04:46
浏览 35
已采纳

转换/保存用户在数据库中提交“时间”

I have a form where users select a time from a form

    <select name='hour'>
    <option value='1'>1</option>
    <option value='2'>2</option>
    <option value='3'>3</option>
    <option value='4'>4</option>
    <option value='5'>5</option>
    <option value='6'>6</option>
    <option value='7'>7</option>
    <option value='8'>8</option>
    <option value='9'>9</option>
    <option value='10'>10</option>
    <option value='11'>11</option>
    <option value='12'>12</option>
</select>
:
<select name='min'>
    <option value='00'>00</option>
    <option value='15'>15</option>
    <option value='30'>30</option>
    <option value='45'>45</option>

</select>
<select name='ampm'>
    <option value='AM'>AM</option>
    <option value='PM'>PM</option>
</select>

I'm posting this information to a processing page where I need to save the time that the user submitted in the database in DateTime format. The data will need to be stored in the database in the datetime format (2011-06-11 23:11:07) with the date being today's date.

I'm thinking that the data function will some how be able to convert it, but I can't seem to figure it out.

  • 写回答

2条回答 默认 最新

  • dphphvs496524 2011-06-12 04:54
    关注

    Easy enough. Assuming the user enters 1:09pm, then:

    $hour = intval($_REQUEST['hour']); // 1
    $min = intval($_REQUEST['minute']); // 9
    if (($_REQUEST['ampm'] == 'PM') && ($hour <> 12)) { 
       $hour += 12; // adjust for 24 clock, so $hour becomes 13
    }
    
    $timestring = sprintf('%02d:%02d:00', $hour, $min); // 13:09:00
    $datestring = date('Y-m-d'); // 2011-06-12
    
    $timestamp = $datestring . ' ' . $timestring; // 2011-06-12 13:09:00
    

    There's other ways of going about this, but this is probably a bit easier to understand.

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

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)