MySQL 如何在mysql数据库中存储日期?

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

how store date in mysql database?

mysqlphpmyadmin

提问by Simon

i have date in dd/mm/yyyyformat. how can i store it in databse, if i fant to do some operations on them after?

我有dd/mm/yyyy格式的日期。如果我想在之后对它们进行一些操作,我如何将它存储在数据库中?



for example i must find out the rows, where date > something

例如,我必须找出行,其中 date > something

what type i must set to datefield?

我必须设置为什么类型datefield

thanks

谢谢

回答by Mark Byers

To store dates or times in MySQL use date, datetimeor timestamp. I'd recommend the first two for most purposes.

要在 MySQL 中存储日期或时间,请使用date,datetimetimestamp。大多数情况下,我建议使用前两个。

To tell MySQL how to parse your date format use the STR_TO_DATEfunction. Here's an example:

要告诉 MySQL 如何解析您的日期格式,请使用STR_TO_DATE函数。下面是一个例子:

CREATE TABLE table1 (`Date` Date);
INSERT INTO table1 (`Date`) VALUES (STR_TO_DATE('01/05/2010', '%m/%d/%Y'));
SELECT * FROM table1;

Date
2010-01-05

To format the results back into the original form look at the DATE_FORMATfunction. Note that you only need to format it if you want to display it as a string using something other than the default format.

要将结果格式化回原始形式,请查看DATE_FORMAT函数。请注意,如果您想使用默认格式以外的其他内容将其显示为字符串,则只需对其进行格式化。

回答by greenimpala

or just dateif you don't need time information

或者只是date如果您不需要时间信息

回答by botmsh

Use dateif you only care about the date and not about the exact time.

使用date,如果你只关心日期,不是确切的时间。