douyan7916 2014-03-25 12: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 12: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"

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部