Cody Carmichael
Cody Carmichael

Reputation: 187

Insert h:mm pm/am time format into database using MYSQL

I'm trying to insert a time written in h:mm am/pm format into a database that is stored as standard DATETIME format ( hh:mm:ss ) but I can't figure out how to convert the posted time into standard format so the database will accept it.

This is what I've been trying so far:

$title = $_POST['inputTitle'];
$date = $_POST['inputDate'];
$time = date('h:i:s a', strtotime($_POST['inputTime']));
$desc = $_POST['inputDesc'];


//msql query to insert data
$query = "INSERT INTO events(title, date, time, description) VALUES ('$title','$date','$time','$desc')";

but this doesn't work(time still won't submit) any ideas?

Upvotes: 3

Views: 2242

Answers (1)

Sergey  Nazarenko
Sergey Nazarenko

Reputation: 159

You should use TIME type not DATETIME type.

DATETIME format is: yyyy-mm-dd hh:ii:ss

DATETIME format

TIME format is: hh:ii:ss

TIME format

Upvotes: 4

Related Questions