I have a DateTime which I want to store in a Date MySQL column. I am using MySQLi and prepared statements.
When binding parameters, I cannot specify date as a type, only strings and integers.
How do I convert the DateTime to a MySQL date string representation? There is very little documentation on the DateTime class.
I almost closed this as a duplicate of Convert from MySQL datetime to another format with PHP but I think it’s actually the reverse problem. That question asked how to format a date fetched from MySQL, and you’re asking how to format a date for input to a query.
Option 1:
You can format the value to the
YYYY-MM-DD HH:MMformat MySQL wants:Then submit it as a string parameter to the query.
Option 2:
Then you can submit the timestamp value as a numeric parameter to the query.
Option 3:
You can submit a wide variety of string representations of date/time, if you specify the formatting to MySQL’s
STR_TO_DATE()function. For the formatting codes, see theDATE_FORMAT()function.