When inserting a datetime value into a MySQL column using PHP's date() function, it's essential to use the correct format to ensure proper data storage.
The incorrect format date('Y-M-D G:i:s') results in the default "0000-00-00 00:00:00" insertion because MySQL expects a specific numeric format for datetime columns.
The solution is to use the correct date format: date('Y-m-d H:i:s'). This format employs numeric representations for month, day, hour, minutes, and seconds, meeting MySQL's datetime column requirements.
Here's an example:
$date = date('Y-m-d H:i:s'); // Current date and time in the correct MySQL datetime format $query = "INSERT INTO mytable (datetime_column) VALUES ('$date')";
By using the correct date() format, you can accurately insert datetime values into MySQL tables and avoid the "0000-00-00 00:00:00" default.
The above is the detailed content of What's the Correct PHP `date()` Format for MySQL `datetime` Column Insertion?. For more information, please follow other related articles on the PHP Chinese website!