I have the following snippet of code:
date_default_timezone_set('UTC');
if (!isset($_POST['secret']) && $post_msg != "" ) { // checkbox unchecked processing...
// Checkbox is selected
$date_of_msg= date('l jS F Y h:i');
$msg_sent_by = $username;
$insert_query = "INSERT INTO user_thoughts VALUES ('','$post_msg','$date_of_msg','','' ,'$attach_name','$msg_sent_by','yes')";
$run_query = mysqli_query($connect, $insert_query) or die(mysqli_error());
}
When I echo $date_of_msg
, the date and time will print out as expected, but when the following INSERT
query above is ran, the field in the db will store 0000-00-00 00:00:00
.
The field which will store $date_of_msg
is called post_details
and is of type datetime
. I am aware that there is a function called date_to_str
and have seem questions related to it, such as this one. But the answers in that question are converting manually inputted dates, whereas I want to get the time when a user makes a post. I think the solution is to use the date_to_str
function when inserting the $date_of_msg
variable? But I am unable to understand how it works?