I have a PHP with a JS datepicker for date and time that save all the string in this format “19.08.2012 @ 20:18” in a a variable called $date.
If I place this variable directly in the query, it writes “0000-00-00 00:00:00” in the database.
At the moment I have a big black hole in my head, so need help.
How can I write the correct selected date and time in the database?
Sorry I thought not so important, here is the code.
index.php
<script type="text/javascript">
$(function() {
$("#date").datetimepicker({
dateFormat:'dd.mm.yy',
separator: ' @ '
});
});
</script>
<form action="setting.php" method="post">
<table>
<tr>
<td><label for="date">Date:</label></td>
<td><input type="text" id="date" name="date"/></td>
</tr>
</table>
<input type="submit" name="submit" />
setting.php
if(isset($_POST['save_autoresp'])) {
create($_POST['date']);
header("Location: admin.php");
exit();
} else {
header("Location: index.php");
exit();
}
functions.php
.......
.......
// Write the fields to the table
$query_create = mysqli_query($link, "INSERT INTO table VALUES (null, '$id', '$date')") or die ('Could not connect: ' . mysqli_error($link));
.......
.......
The date field in teh MySQL database is “datetime”.
The date you receive is in a format that MySQL doesn’t recognize. You must first transform it into an accepted date and time literal, then you can pass it to the INSERT.
This conversion can be done in two ways:
Whichever you’ll choose, stick to the Year-Month-Day format for the dates when saving to the database, so that you won’t get crazy in finding out why “08/02” was considered the 2nd of August instead of the 8th of February.