Alex Alex - 1 year ago 92
PHP Question

PHP date() format when inserting into datetime in MySQL

Whats the correct format using the date() function in PHP to insert into a MySQL datetime type column?

I've been trying this date("Y-M-D G:i:s"); but that just inserts "0000-00-00 00:00:00" everytime.


The problem is that you're using 'M' and 'D', which are a textual representations, MySQL is expecting a numeric representation of the format 2010-02-06 19:30:13

Try: date("Y-m-d H:i:s") which uses the numeric equivalents.

edit: switched G to H, though it may not have impact, you probably want to use 24-hour format with leading 0s.