开发者

How to insert date(y-m-d) into MySQL database?

I have a field of type date in MySQL. When I try to insert any date in this f开发者_运维知识库ield with PHP using following query, It stores 0000-00-00 in that field.

For example:

UPDATE test SET dob=2000-09-20 WHERE id=3


Just quote the date.

UPDATE test SET dob='2000-09-20' WHERE id=3

In your query ... 2000-09-20 ... will be interpreted as a mathematical expression. The result is a number, 1971. In the date time field, a number will be zerofilled to a 6, 8, 12 or 14-digit number, so 1971 will become 001971. This number is then interpreted as YYMMDD format, so it means "year 00 month 19 day 71", which is invalid. So the special value 0000-00-00 is stored.


Normally, you would use '2000-09-20' as the value to insert. How does your system distinguish between what you have and the integer (non-date) value 1971, which is what you'd get by subtracting 9 and 20 from 2000?

UPDATE test SET dob = '2000-09-20' WHERE id = 3

See here for more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜