in my php code I was asking the user to choose day, month and year from some dropdown fields where values where 1, 2, 3 etc instead of 01, 02, 03.
these were then combined to form a string like “YYYY-MM-DD” for the insertion in a db (in a date field).
Having missed the initial 0, I thought I was sending strings in the wrong format, eg “YYYY-M-D” or YYYY-MM-D”, but then I’ve noticed they appear in the right format in the database anyway: even if I submitted YYYY-M-D, it appeared as YYYY-MM-DD.
is this the normal behaviour of mysql? if so, can i just avoid worrying about changing the code in my application?
Yes, this is normal behaviour. If your column has type DATE, MySQL will convert the string to an internal representation of a date when inserting it. MySQL is flexible when parsing dates as long as the order is correct (year, month, day). When displaying dates it will use ‘YYYY-MM-DD’ regardless of the format you used when storing the data.
Here are some quotes from the relevant page of the manual: