The date and time data types for representing temporal values are 用于表示时间值的日期和时间数据类型是DATE
, TIME
, DATETIME
, TIMESTAMP
, and YEAR
. DATE
、TIME
、DATETIME
、TIMESTAMP
和YEAR
。Each temporal type has a range of valid values, as well as a “zero” value that may be used when you specify an invalid value that MySQL cannot represent. 每个时态类型都有一系列有效值,以及一个“零”值,当您指定MySQL无法表示的无效值时,可以使用该值。The TIMESTAMP
and DATETIME
types have special automatic updating behavior, described in Section 11.2.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.TIMESTAMP
和DATETIME
类型具有特殊的自动更新行为,如第11.2.5节“TIMESTAMP和DATETIME的自动初始化和更新”所述。
For information about storage requirements of the temporal data types, see Section 11.7, “Data Type Storage Requirements”.有关临时数据类型的存储要求的信息,请参阅第11.7节,“数据类型存储要求”。
For descriptions of functions that operate on temporal values, see Section 12.7, “Date and Time Functions”.有关对时间值进行操作的函数的描述,请参阅第12.7节,“日期和时间函数”。
Keep in mind these general considerations when working with date and time types:使用日期和时间类型时,请记住以下一般注意事项:
MySQL retrieves values for a given date or time type in a standard output format, but it attempts to interpret a variety of formats for input values that you supply (for example, when you specify a value to be assigned to or compared to a date or time type). MySQL以标准输出格式检索给定日期或时间类型的值,但它尝试为您提供的输入值解释各种格式(例如,当您指定要分配给日期或时间类型或与之比较的值时)。For a description of the permitted formats for date and time types, see Section 9.1.3, “Date and Time Literals”. 有关允许的日期和时间类型格式的说明,请参阅第9.1.3节,“日期和时间文字”。It is expected that you supply valid values. 希望您提供有效值。Unpredictable results may occur if you use values in other formats.如果使用其他格式的值,可能会出现不可预知的结果。
Although MySQL tries to interpret values in several formats, date parts must always be given in year-month-day order (for example, 尽管MySQL尝试以几种格式解释值,但日期部分必须始终以年-月-日顺序(例如,'98-09-04'
), rather than in the month-day-year or day-month-year orders commonly used elsewhere (for example, '09-04-98'
, '04-09-98'
). '98-09-04'
)给出,而不是其他地方常用的月-日-年或日-月-年顺序(例如,'09-04-98'
、'04-09-98'
)。To convert strings in other orders to year-month-day order, the 要将其他顺序中的字符串转换为年-月-日顺序,STR_TO_DATE()
function may be useful.STR_To_DATE()
函数可能很有用。
Dates containing 2-digit year values are ambiguous because the century is unknown. 包含两位数年份值的日期不明确,因为世纪未知。MySQL interprets 2-digit year values using these rules:MySQL使用以下规则解释两位数的年份值:
Year values in the range 70-99
become 1970-1999
.70-99
之间的年份值变为1970-1999
。
Year values in the range 范围为00-69
become 2000-2069
.00-69
的年份值变为2000-2069
。
See also Section 11.2.8, “2-Digit Years in Dates”.另请参见第11.2.8节,“日期中的两位数年份”。
Conversion of values from one temporal type to another occurs according to the rules in Section 11.2.7, “Conversion Between Date and Time Types”.根据第11.2.7节,“日期和时间类型之间的转换”中的规则,将值从一种时间类型转换为另一种时间类型。
MySQL automatically converts a date or time value to a number if the value is used in numeric context and vice versa.如果在数字上下文中使用日期或时间值,MySQL会自动将其转换为数字,反之亦然。
By default, when MySQL encounters a value for a date or time type that is out of range or otherwise invalid for the type, it converts the value to the “zero” value for that type. 默认情况下,当MySQL遇到日期或时间类型的值超出范围或对该类型无效时,它会将该值转换为该类型的“零”值。The exception is that out-of-range 例外情况是超出范围的TIME
values are clipped to the appropriate endpoint of the TIME
range.TIME
值被剪裁到TIME
范围的适当端点。
By setting the SQL mode to the appropriate value, you can specify more exactly what kind of dates you want MySQL to support. 通过将SQL mode设置为适当的值,可以更准确地指定希望MySQL支持的日期类型。(See Section 5.1.11, “Server SQL Modes”.) (请参阅第5.1.11节“服务器SQL模式”。)You can get MySQL to accept certain dates, such as 您可以让MySQL接受某些日期,例如“2009-11-31”,方法是启用'2009-11-31'
, by enabling the ALLOW_INVALID_DATES
SQL mode. ALLOW_INVALID_DATES
SQL模式。This is useful when you want to store a “possibly wrong” value which the user has specified (for example, in a web form) in the database for future processing. 当您希望在数据库中存储用户指定的“可能错误”值(例如,在web表单中)以供将来处理时,这非常有用。Under this mode, MySQL verifies only that the month is in the range from 1 to 12 and that the day is in the range from 1 to 31.在这种模式下,MySQL只验证月在1到12之间,天在1到31之间。
MySQL permits you to store dates where the day or month and day are zero in a MySQL允许您在DATE
or DATETIME
column. DATE
或DATETIME
列中存储日或月和日为零的日期。This is useful for applications that need to store birthdates for which you may not know the exact date. 这对于需要存储您可能不知道确切日期的生日的应用程序非常有用。In this case, you simply store the date as 在本例中,您只需将日期存储为'2009-00-00'
or '2009-01-00'
. '2009-00-00'
或'2009-01-00'
。However, with dates such as these, you should not expect to get correct results for functions such as 但是,对于这样的日期,对于需要完整日期的函数,例如DATE_SUB()
or DATE_ADD()
that require complete dates. DATE_SUB()
或DATE_ADD()
,您不应该期望得到正确的结果。To disallow zero month or day parts in dates, enable the 若要禁止日期中的零月或零日部分,请启用NO_ZERO_IN_DATE
mode.NO_ZERO_IN_DATE
模式。
MySQL permits you to store a “zero” value of MySQL允许您将'0000-00-00'
as a “dummy date.” '0000-00-00'
的“零”值存储为“虚拟日期”In some cases, this is more convenient than using 在某些情况下,这比使用NULL
values, and uses less data and index space. NULL
值更方便,并且使用更少的数据和索引空间。To disallow 若要禁用'0000-00-00'
, enable the NO_ZERO_DATE
mode.'0000-00-00'
,请启用NO_ZERO_DATE
模式。
“Zero” date or time values used through Connector/ODBC are converted automatically to 通过连接器/ODBC使用的“零”日期或时间值会自动转换为NULL
because ODBC cannot handle such values.NULL
,因为ODBC无法处理此类值。
The following table shows the format of the “zero” value for each type. 下表显示了每种类型的“零”值的格式。The “zero” values are special, but you can store or refer to them explicitly using the values shown in the table. “零”值是特殊的,但是您可以使用表中显示的值显式地存储或引用它们。You can also do this using the values 也可以使用值'0'
or 0
, which are easier to write. '0'
或0
来执行此操作,这些值更易于编写。For temporal types that include a date part (对于包含日期部分(DATE
, DATETIME
, and TIMESTAMP
), use of these values may produce warning or errors. DATE
、DATETIME
和TIMESTAMP
)的时态类型,使用这些值可能会产生警告或错误。The precise behavior depends on which, if any, of the strict and 精确的行为取决于启用了哪种严格和NO_ZERO_DATE
SQL modes are enabled; see Section 5.1.11, “Server SQL Modes”.NO_ZERO_DATE
SQL模式(如果有的话);请参阅第5.1.11节,“服务器SQL模式”。