I am looking for a solution to capturing a users input from a HTML input set to a value of 'datetime-local' into a MySQL database with a table field set to 'datetime'. Whenever I submit the form an ID is generated and the date is saved, but the time is not. EX: 2018-11-20 00:00:00.
My HTML:
<form class="" action="/index.php" method="post">
<input type="datetime-local" name="myDate" id="myDate"><br /><br />
<button id="submit" value="submit">Submit</button><br /><br />
</form>
My PHP:
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$sql = "INSERT INTO test_one ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['myDate'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
I got the code from a tutorial on Youtube, and it is working so far. I just want to be able to get the time that they select as well.
UPDATE: The time is being saved, but it appears to be in military time. EX: Input = 11/22/2018 5:00 PM ; Output = 2018-11-22 17:00:00
UPDATE AFTER FIRST COMMENT: This directs me to a failed HTTP request -
// Check Our Connection
if ( $mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
}
// Insert Our Data
$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
$sql = "INSERT INTO test_one ( mydate ) VALUES ( '{$mysqli->real_escape_string($_POST['$date'])}' )";
$insert = $mysqli->query($sql);
// Print Response from MySQL
if ( $insert ) {
echo "Success! Row ID: {$mysqli->insert_id}";
} else {
die("Error: {$mysqli->errno} : {$mysqli->error}");
}
// Close Connection
$mysqli->close();
If it has anything to do with ' ' around $date I have tried it without and got the same error.
Thank you in advance!