如何将自定义日期插入 mysql 时间戳字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18092303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:27:42  来源:igfitidea点击:

How to Insert custom date into mysql timestamp field?

mysqldatetimetimestampunix-timestamp

提问by Maximus

I have tried to insert date and time string formatted into mysql timestamp field by using following two methods but both shows me 0000-00-00 00:00:00

我尝试使用以下两种方法将格式化的日期和时间字符串插入到 mysql 时间戳字段中,但都显示 0000-00-00 00:00:00

INSERT INTO test VALUES ( UNIX_TIMESTAMP('2013-08-05 18:19:03') )
INSERT INTO test VALUES ( UNIX_TIMESTAMP(STR_TO_DATE('2013-08-05 18:19:03', '%Y-%m-%d %H:%i:%s')) )

I believe first one should work as I am expecting but not sure why isn't parsing date and time?

我相信第一个应该像我期望的那样工作,但不确定为什么不解析日期和时间?

回答by SaganRitual

The problem is that your field is defined as TIMESTAMPbut UNIX_TIMESTAMPreturns an int. Use INSERT INTO test VALUES ('2013-08-05 18:19:03' )instead.

问题是您的字段被定义为TIMESTAMPUNIX_TIMESTAMP返回一个整数。使用INSERT INTO test VALUES ('2013-08-05 18:19:03' )来代替。