Sven van den Boogaart
Sven van den Boogaart

Reputation: 12327

Convert date and time input to 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

Upvotes: 2

Views: 7055

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

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"

Upvotes: 4

Related Questions