php 在 MySQL 中插入 mm/dd/yyyy 格式的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/624009/
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-24 23:17:43  来源:igfitidea点击:

Inserting mm/dd/yyyy format dates in MySQL

phpmysql

提问by MarkR

How can I insert an MM/DD/YYYY format date into an MySQL database using PHP?

如何使用 PHP 将 MM/DD/YYYY 格式的日期插入到 MySQL 数据库中?

回答by Jan Jungnickel

This can be done using STR_TO_DATE.

这可以使用STR_TO_DATE来完成。

Example:

例子:

INSERT INTO useless_table (id, date_added) VALUES(
            1, STR_TO_DATE('03/08/2009', '%m/%d/%Y'));

EDIT: Please also consider MarkR's solution, because it's the right thing to do[tm].

编辑:还请考虑 MarkR 的解决方案,因为这是正确的做法 [tm]。

回答by MarkR

MySQL support ISO-8601 date/time values, and no others. If you need to use some other whacky format (for example, because you have American customers who expect wonky dates), you need to do the conversion yourself.

MySQL 支持 ISO-8601 日期/时间值,没有其他的。如果您需要使用其他一些奇怪的格式(例如,因为您的美国客户希望日期不稳定),则需要自己进行转换。

Just live with it, ISO-8601 is the one true date format.

接受它,ISO-8601 是一种真正的日期格式。