Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

time

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?

like image 499
Cody Carmichael Avatar asked Jan 20 '26 08:01

Cody Carmichael


1 Answers

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

like image 180
Sergey Nazarenko Avatar answered Jan 23 '26 01:01

Sergey Nazarenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!