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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据