0

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!

1 Answer 1

2

The issue is that the datetime-local adds a T in the middle, i.e. 2018-11-19T10:52:23. You have to change the format to remove the T. Maybe try this:

$date = strftime('%Y-%m-%d %H:%M:%S', strtotime(real_escape_string($_POST['myDate'])));
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately that did not work for me. Thank you though! I placed it above the $sql line then update the $_POST after the VALUE to capture $date, but after I submitted the form I got an HTTP error. @miken32
@EataSandwhich Did you use it as $_POST['date']? As that won't work it would need to be just $date. i.e. VALUES ($date)
Ill update my question to include the new code @Dan W.
@EataSandwhich The time 'military time' is how servers treat datetime, you have to convert it back after you pull it out of the database.
Oh.... ok. so I guess I am good with that then, right? If you can't tell I am an expert at this hahah @Dan W.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.