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
Release: 2024-12-02 09:08:08
Original
164 people have browsed it

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')";
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template