PHP: Date Format for MySQL INSERT
Question by Compi | 2018-05-12 at 20:43
I want to write a date into a DATETIME or TIMESTAMP column of a MySQL table in my database. Which format should I use for PHP's date() function for MySQL to accept the date and insert it correctly?
Related Topics
MySQL: Write current Date or Time into Column
Tutorial | 0 Comments
PHP: Current Date and Time
Tutorial | 0 Comments
PHP: Determine Week Number
Tutorial | 0 Comments
MySQL/PHP: How to retrieve the last INSERT ID
Info | 0 Comments
Delphi/Lazarus: Display current Date and Time
Tip | 0 Comments
JavaScript: Get current Date and Time
Tutorial | 1 Comment
VirtualBox: Change Date and Time
Tutorial | 10 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
MySQL requires a date in the format YYYY-MM-DD SS-MM-SS. You get this format with the format string "Y-m-d H:i:s":
When inserting, make sure to put the date in quotation marks. So for example dat = '$date' as in the example above.
Incidentally, you can also omit the time part and, for example, only use date("Y-m-d"). Then MySQL uses 00:00:00 as a timestamp.
2018-05-12 at 22:29