douyan7916 2014-03-25 20:25
浏览 296
已采纳

将日期和时间输入转换为sql datetime

I am makeing a form where u can select a date and a time to make an apointment. The fields are of the type date and time.

 <input type="date" name="date" placeholder="The date">
 <input type="time" name="time"  placeholder="The time">

I want to convert this input to a datetime format wich can be inserted with mysqli. what i tryd

  $datetime = $date . $time

but it isn't a valid datetime. How can i convert it to a valid datetime so i can insert it in the database ?

edit

if i echo them after each other with a " " space between them i get 2018-05-05 03:05

the insert finishes but the data in the database is 0000-00-00 00:00:00 instead of the right date

  • 写回答

1条回答 默认 最新

  • duan2891 2014-03-25 20:26
    关注

    In php the concate operator is . not +

    so you need to do as

    $datetime = $date.' '.$time;
    

    If your date column in DB is Y-m-d H:i:s or in other words DATETIME then you can do as

    $datetime = $date.' '.$time
    $datetime = date("Y-m-d H:i:s",strtotime($datetime));
    

    Now while inserting make sure you tread date as string.

    In Direct insert you need to wrap the variable with quotes , in prepared statement you should bind param as "s"

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

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试