Home >Database >Mysql Tutorial >What's the Correct PHP `date()` Format for MySQL `datetime` Column Insertion?

What's the Correct PHP `date()` Format for MySQL `datetime` Column Insertion?

Susan Sarandon
Susan SarandonOriginal
2024-12-02 09:08:08191browse

What's the Correct PHP `date()` Format for MySQL `datetime` Column Insertion?

Correct PHP date() Format for MySQL datetime Column Insertion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn