Home > Database > Mysql Tutorial > How to Correctly Format Dates for MySQL datetime Insertion Using PHP's `date()` Function?

How to Correctly Format Dates for MySQL datetime Insertion Using PHP's `date()` Function?

DDD
Release: 2024-12-07 02:22:10
Original
928 people have browsed it

How to Correctly Format Dates for MySQL datetime Insertion Using PHP's `date()` Function?

Handling Date Formats for MySQL Datetime Insertion Using PHP's date() Function

When inserting date values into a MySQL datetime column using PHP's date() function, it's crucial to utilize the correct format. A common issue arises when attempting to insert a date using 'Y-M-D G:i:s' as the format, resulting in "0000-00-00 00:00:00" being stored.

The root of this problem lies in the use of 'M' and 'D' to represent months and days in a textual format. MySQL expects numeric representations in the format YYYY-MM-DD HH:MM:SS.

To resolve this issue, simply switch to the numeric equivalents:

Correct Format: date('Y-m-d H:i:s')

This format specifies a numeric representation of the year ('Y'), month ('m'), day ('d'), hour ('H'), minute ('i'), and second ('s').

Note: The original format ('Y-M-D G:i:s') used 'G' for hours, which displays 12-hour time without leading zeros. It's generally recommended to use 'H' for 24-hour time with leading zeros to avoid potential formatting issues.

The above is the detailed content of How to Correctly Format Dates for MySQL datetime Insertion Using PHP's `date()` Function?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template