This section describes the functions that can be used to manipulate temporal values. 本节描述可用于操作时间值的函数。See Section 11.2, “Date and Time Data Types”, for a description of the range of values each date and time type has and the valid formats in which values may be specified.请参阅第11.2节,“日期和时间数据类型”,以了解每个日期和时间类型的值范围以及指定值的有效格式。
Table 12.11 Date and Time Functions日期和时间函数
ADDDATE() | |
ADDTIME() | |
CONVERT_TZ() | |
CURDATE() | |
CURRENT_DATE(), CURRENT_DATE | CURDATE()的同义词 |
CURRENT_TIME(), CURRENT_TIME | CURTIME()的同义词 |
CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP | NOW()的同义词 |
CURTIME() | |
DATE() | |
DATE_ADD() | |
DATE_FORMAT() | |
DATE_SUB() | |
DATEDIFF() | |
DAY() | DAYOFMONTH()的同义词 |
DAYNAME() | |
DAYOFMONTH() | |
DAYOFWEEK() | |
DAYOFYEAR() | |
EXTRACT() | |
FROM_DAYS() | |
FROM_UNIXTIME() | |
GET_FORMAT() | |
HOUR() | |
LAST_DAY | |
LOCALTIME(), LOCALTIME | NOW()的同义词 |
LOCALTIMESTAMP, LOCALTIMESTAMP() | NOW()的同义词 |
MAKEDATE() | |
MAKETIME() | |
MICROSECOND() | |
MINUTE() | |
MONTH() | |
MONTHNAME() | |
NOW() | |
PERIOD_ADD() | |
PERIOD_DIFF() | |
QUARTER() | |
SEC_TO_TIME() | |
SECOND() | |
STR_TO_DATE() | |
SUBDATE() | DATE_SUB()的同义词 |
SUBTIME() | |
SYSDATE() | |
TIME() | |
TIME_FORMAT() | |
TIME_TO_SEC() | |
TIMEDIFF() | |
TIMESTAMP() | |
TIMESTAMPADD() | |
TIMESTAMPDIFF() | |
TO_DAYS() | |
TO_SECONDS() | |
UNIX_TIMESTAMP() | |
UTC_DATE() | |
UTC_TIME() | |
UTC_TIMESTAMP() | |
WEEK() | |
WEEKDAY() | |
WEEKOFYEAR() | |
YEAR() | |
YEARWEEK() |
Here is an example that uses date functions. 下面是一个使用日期函数的示例。the following query selects all rows with a 以下查询选择最近30天内具有date_col value from within the last 30 days:date_col值的所有行:
mysql>SELECT->somethingFROMtbl_nameWHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <=date_col;
the query also selects rows with dates that lie in the future.查询还选择日期在未来的行。
Functions that expect date values usually accept datetime values and ignore the time part. 期望日期值的函数通常接受日期时间值并忽略时间部分。Functions that expect time values usually accept datetime values and ignore the date part.期望时间值的函数通常接受datetime值并忽略日期部分。
Functions that return the current date or time each are evaluated only once per query at the start of query execution. 每次返回当前日期或时间的函数在查询执行开始时每个查询只计算一次。This means that multiple references to a function such as 这意味着在单个查询中对函数的多个引用(如NOW() within a single query always produce the same result. NOW())总是产生相同的结果。(For our purposes, a single query also includes a call to a stored program (stored routine, trigger, or event) and all subprograms called by that program.) (出于我们的目的,单个查询还包括对存储程序(存储例程、触发器或事件)的调用以及该程序调用的所有子程序。)This principle also applies to 这一原则也适用于CURDATE(), CURTIME(), UTC_DATE(), UTC_TIME(), UTC_TIMESTAMP(), and to any of their synonyms.CURDATE()、CURTIME()、UTC_DATE()、UTC_TIME()、UTC_TIMESTAMP()及其任何同义词。
the CURRENT_TIMESTAMP(), CURRENT_TIME(), CURRENT_DATE(), and FROM_UNIXTIME() functions return values in the current session time zone, which is available as the session value of the time_zone system variable. CURRENT_TIMESTAMP()、CURRENT_TIME()、CURRENT_DATE()和FROM_UNIXTIME()函数返回当前会话时区中的值,该值可用作time_zone系统变量的会话值。In addition, 此外,UNIX_TIMESTAMP() assumes that its argument is a datetime value in the session time zone. UNIX_TIMESTAMP()假定其参数是会话时区中的日期时间值。See Section 5.1.15, “MySQL Server Time Zone Support”.请参阅第5.1.15节,“MySQL服务器时区支持”。
Some date functions can be used with “zero” dates or incomplete dates such as 一些日期函数可以与“零”日期或不完整日期(如'2001-11-00', whereas others cannot. '2001-11-00')一起使用,而其他函数则不能。Functions that extract parts of dates typically work with incomplete dates and thus can return 0 when you might otherwise expect a nonzero value. 提取部分日期的函数通常处理不完整的日期,因此当您可能期望非零值时,可以返回0。For example:例如:
mysql> SELECT DAYOFMONTH('2001-11-00'), MONTH('2005-00-00');
-> 0, 0
Other functions expect complete dates and return 其他函数需要完整的日期,不完整的日期返回NULL for incomplete dates. NULL。These include functions that perform date arithmetic or that map parts of dates to names. 这些函数包括执行日期算术或将部分日期映射到名称的函数。For example:例如:
mysql>SELECT DATE_ADD('2006-05-00',INTERVAL 1 DAY);-> NULL mysql>SELECT DAYNAME('2006-05-00');-> NULL
Several functions are strict when passed a 有几个函数在传递DATE()函数值作为参数时是严格的,并拒绝日期部分为零的不完整日期:DATE() function value as their argument and reject incomplete dates with a day part of zero: CONVERT_TZ(), DATE_ADD(), DATE_SUB(), DAYOFYEAR(), TIMESTAMPDIFF(), TO_DAYS(), TO_SECONDS(), WEEK(), WEEKDAY(), WEEKOFYEAR(), YEARWEEK().CONVERT_TZ()、DATE_ADD()、DATE_SUB()、DAYOFYEAR()、TIMESTAMPDIFF()、TO_DAYS()、TO_SECONDS()、WEEK()、WEEKDAY()、WEEKOFYEAR()、YEARWEEK()。
Fractional seconds for 支持TIME, DATETIME, and TIMESTAMP values are supported, with up to microsecond precision. TIME、DATETIME和TIMESTAMP值的分秒,精度高达微秒。Functions that take temporal arguments accept values with fractional seconds. 采用时态参数的函数接受带小数秒的值。Return values from temporal functions include fractional seconds as appropriate.时间函数的返回值包括适当的小数秒。
ADDDATE(, date,INTERVAL expr unit)ADDDATE(expr,days)
When invoked with the 当使用第二个参数的间隔形式调用时,INTERVAL form of the second argument, ADDDATE() is a synonym for DATE_ADD(). ADDDATE()是DATE_ADD()的同义词。the related function 相关函数SUBDATE() is a synonym for DATE_SUB(). SUBDATE()是DATE_SUB()的同义词。For information on the 有关INTERVAL unit argument, see Temporal Intervals.INTERVAL unit参数的信息,请参见时间间隔。
mysql>SELECT DATE_ADD('2008-01-02', INTERVAL 31 DAY);-> '2008-02-02' mysql>SELECT ADDDATE('2008-01-02', INTERVAL 31 DAY);-> '2008-02-02'
When invoked with the 当使用第二个参数的days form of the second argument, MySQL treats it as an integer number of days to be added to expr.days形式调用时,MySQL将其视为要添加到expr的整数天数。
mysql> SELECT ADDDATE('2008-01-02', 31);
-> '2008-02-02'ADDTIME() adds expr2 to expr1 and returns the result. ADDTIME()将expr2添加到expr1并返回结果。expr1 is a time or datetime expression, and expr2 is a time expression.expr1是时间或日期时间表达式,expr2是时间表达式。
mysql>SELECT ADDTIME('2007-12-31 23:59:59.999999', '1 1:1:1.000002');-> '2008-01-02 01:01:01.000001' mysql>SELECT ADDTIME('01:00:00.999999', '02:00:00.999998');-> '03:00:01.999997'
CONVERT_TZ() converts a datetime value dt from the time zone given by from_tz to the time zone given by to_tz and returns the resulting value. CONVERT_TZ()将日期时间值dt从from_TZ指定的时区转换为to_TZ指定的时区,并返回结果值。Time zones are specified as described in Section 5.1.15, “MySQL Server Time Zone Support”. 时区的指定如第5.1.15节,“MySQL服务器时区支持”所述。This function returns 如果参数无效,此函数将返回NULL if the arguments are invalid.NULL。
If the value falls out of the supported range of the 如果值在TIMESTAMP type when converted from from_tz to UTC, no conversion occurs. from_tz转换为UTC时超出了TIMESTAMP类型的支持范围,则不会发生转换。the TIMESTAMP range is described in Section 11.2.1, “Date and Time Data Type Syntax”.TIMESTAMP范围在第11.2.1节,“日期和时间数据类型语法”中描述。
mysql>SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');-> '2004-01-01 13:00:00' mysql>SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');-> '2004-01-01 22:00:00'
To use named time zones such as 要使用命名时区,如'MET' or 'Europe/Amsterdam', the time zone tables must be properly set up. 'MET'或'Europe/Amsterdam',必须正确设置时区表。For instructions, see Section 5.1.15, “MySQL Server Time Zone Support”.有关说明,请参阅第5.1.15节,“MySQL服务器时区支持”。
Returns the current date as a value in 将当前日期以' or YYYY-MM-DD'YYYYMMDD format, depending on whether the function is used in string or numeric context.'格式或YYYY-MM-DD'YYYYMMDD格式的值返回,具体取决于函数是在字符串上下文中使用还是在数字上下文中使用。
mysql>SELECT CURDATE();-> '2008-06-13' mysql>SELECT CURDATE() + 0;-> 20080613
CURRENT_DATE and CURRENT_DATE() are synonyms for CURDATE().CURRENT_DATE和CURRENT_DATE()是CURDATE()的同义词。
CURRENT_TIME, CURRENT_TIME([fsp])
CURRENT_TIME and CURRENT_TIME() are synonyms for CURTIME().CURRENT_TIME和CURRENT_TIME()是CURTIME()的同义词。
CURRENT_TIMESTAMP, CURRENT_TIMESTAMP([fsp])
CURRENT_TIMESTAMP and CURRENT_TIMESTAMP() are synonyms for NOW().CURRENT_TIMESTAMP和CURRENT_TIMESTAMP()是NOW()的同义词。
Returns the current time as a value in 将当前时间以'hh:mm:ss' or hhmmss format, depending on whether the function is used in string or numeric context. 'hh:mm:ss'格式或hhmmss格式的值返回,具体取决于函数是在字符串上下文或数字上下文中使用。the value is expressed in the session time zone.该值以会话时区表示。
If the 如果给定fsp argument is given to specify a fractional seconds precision from 0 to 6, the return value includes a fractional seconds part of that many digits.fsp参数以指定0到6之间的分秒精度,则返回值将包含该多个数字中的分秒部分。
mysql>SELECT CURTIME();-> '23:50:26' mysql>SELECT CURTIME() + 0;-> 235026.000000
Extracts the date part of the date or datetime expression 提取日期或日期时间表达式expr.expr的日期部分。
mysql> SELECT DATE('2003-12-31 01:02:03');
-> '2003-12-31'DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. DATEDIFF()返回expr1 − expr2,表示为从一个日期到另一个日期的天数值。expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.expr1和expr2是日期或日期和时间表达式。计算中仅使用值的日期部分。
mysql>SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');-> 1 mysql>SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');-> -31
DATE_ADD(, date,INTERVAL expr unit)DATE_SUB(date,INTERVAL expr unit)
These functions perform date arithmetic. 这些函数执行日期运算。the date argument specifies the starting date or datetime value. date参数指定开始日期或datetime值。expr is an expression specifying the interval value to be added or subtracted from the starting date. expr是一个表达式,指定要从开始日期加上或减去的间隔值。expr is evaluated as a string; it may start with a - for negative intervals. expr作为字符串计算;它可能以-开始,表示负间隔。unit is a keyword indicating the units in which the expression should be interpreted.unit是一个关键字,指示表达式应在其中进行解释的单位。
For more information about temporal interval syntax, including a full list of 有关时间间隔语法的详细信息,包括unit specifiers, the expected form of the expr argument for each unit value, and rules for operand interpretation in temporal arithmetic, see Temporal Intervals.unit说明符的完整列表、每个unit值的expr参数的预期形式以及时间算术中操作数解释的规则,请参见时间间隔。
the return value depends on the arguments:返回值取决于参数:
如果DATE if the date argument is a DATE value and your calculations involve only YEAR, MONTH, and DAY parts (that is, no time parts).date参数是DATE值,并且您的计算只涉及YEAR、MONTH和DAY部分(即没有时间部分),则为DATE。
如果第一个参数是DATETIME if the first argument is a DATETIME (or TIMESTAMP) value, or if the first argument is a DATE and the unit value uses HOURS, MINUTES, or SECONDS.DATETIME(或TIMESTAMP)值,或者第一个参数是DATE,并且unit值使用HOURS、MINUTES或SECONDS,则为DATETIME。
String otherwise.否则为字符串。
To ensure that the result is 为了确保结果是DATETIME, you can use CAST() to convert the first argument to DATETIME.DATETIME,可以使用CAST()将第一个参数转换为DATETIME。
mysql>SELECT DATE_ADD('2018-05-01',INTERVAL 1 DAY);-> '2018-05-02' mysql>SELECT DATE_SUB('2018-05-01',INTERVAL 1 YEAR);-> '2017-05-01' mysql>SELECT DATE_ADD('2020-12-31 23:59:59',->INTERVAL 1 SECOND);-> '2021-01-01 00:00:00' mysql>SELECT DATE_ADD('2018-12-31 23:59:59',->INTERVAL 1 DAY);-> '2019-01-01 23:59:59' mysql>SELECT DATE_ADD('2100-12-31 23:59:59',->INTERVAL '1:1' MINUTE_SECOND);-> '2101-01-01 00:01:00' mysql>SELECT DATE_SUB('2025-01-01 00:00:00',->INTERVAL '1 1:1:1' DAY_SECOND);-> '2024-12-30 22:58:59' mysql>SELECT DATE_ADD('1900-01-01 00:00:00',->INTERVAL '-1 10' DAY_HOUR);-> '1899-12-30 14:00:00' mysql>SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);-> '1997-12-02' mysql>SELECT DATE_ADD('1992-12-31 23:59:59.000002',->INTERVAL '1.999999' SECOND_MICROSECOND);-> '1993-01-01 00:00:01.000001'
Formats the 根据date value according to the format string.format字符串设置date值的格式。
the specifiers shown in the following table may be used in the 下表中显示的说明符可用于format string. format字符串。the 格式说明符字符之前需要% character is required before format specifier characters. %字符。the specifiers apply to other functions as well: 这些说明符也适用于其他函数:STR_TO_DATE(), TIME_FORMAT(), UNIX_TIMESTAMP().STR_to _DATE()、TIME_FORMAT()、UNIX_TIMESTAMP()。
%a | Sun..Sat) |
%b | Jan..Dec) |
%c | 0..12) |
%D | 0th, 1st, 2nd, 3rd, …) |
%d | 00..31) |
%e | 0..31) |
%f | 000000..999999) |
%H | 00..23) |
%h | 01..12) |
%I | 01..12) |
%i | 00..59) |
%j | 001..366) |
%k | 0..23) |
%l | 1..12) |
%M | January..December) |
%m | 00..12) |
%p | AMPM |
%r | hh:mm:ssAMPM) |
%S | 00..59) |
%s | 00..59) |
%T | hh:mm:ss) |
%U | 00..53)WEEK() mode 0WEEK()模式0 |
%u | 00..53)WEEK() mode 1WEEK()模式1 |
%V | 01..53)WEEK() mode 2; used with %XWEEK()模式2;与%X一起使用 |
%v | 01..53)WEEK() mode 3; used with %xWEEK()模式3;与%x一起使用 |
%W | Sunday..Saturday) |
%w | 0=Sunday..6=Saturday) |
%X | %V%V一起使用 |
%x | %v%v一起使用 |
%Y | |
%y | |
%% | % character%字符 |
% | x, for any “x” not listed abovex、 以上未列出的任何“x” |
Ranges for the month and day specifiers begin with zero due to the fact that MySQL permits the storing of incomplete dates such as 月份和日期说明符的范围从零开始,因为MySQL允许存储不完整的日期,例如'2014-00-00'.'2014-00-00'。
the language used for day and month names and abbreviations is controlled by the value of the 用于日和月名称和缩写的语言由lc_time_names system variable (Section 10.16, “MySQL Server Locale Support”).lc_time_names系统变量的值控制(第10.16节,“MySQL服务器区域设置支持”)。
For the 对于%U, %u, %V, and %v specifiers, see the description of the WEEK() function for information about the mode values. %U、%u、%V和%v说明符,请参阅WEEK()函数的说明以获取有关模式值的信息。the mode affects how week numbering occurs.该模式影响周编号的发生方式。
DATE_FORMAT() returns a string with a character set and collation given by character_set_connection and collation_connection so that it can return month and weekday names containing non-ASCII characters.DATE_FORMAT()返回一个字符串,其中包含character_set_connection和collation_connection给定的字符集和排序规则,以便返回包含非ASCII字符的月份和工作日名称。
mysql>SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');-> 'Sunday October 2009' mysql>SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s');-> '22:23:00' mysql>SELECT DATE_FORMAT('1900-10-04 22:23:00',->'%D %y %a %d %m %b %j');-> '4th 00 Thu 04 10 Oct 277' mysql>SELECT DATE_FORMAT('1997-10-04 22:23:00',->'%H %k %I %r %T %S %w');-> '22 22 10 10:23:00 PM 22:23:00 00 6' mysql>SELECT DATE_FORMAT('1999-01-01', '%X %V');-> '1998 52' mysql>SELECT DATE_FORMAT('2006-06-00', '%d');-> '00'
DATE_SUB(date,INTERVAL expr unit)
See the description for 请参见DATE_ADD().DATE_ADD()的说明。
DAY() is a synonym for DAYOFMONTH().DAY()是DAYOFMONTH()的同义词。
Returns the name of the weekday for 返回date. date的星期几名称。the language used for the name is controlled by the value of the 用于名称的语言由lc_time_names system variable (Section 10.16, “MySQL Server Locale Support”).lc_time_names系统变量的值控制(第10.16节,“MySQL服务器区域设置支持”)。
mysql> SELECT DAYNAME('2007-02-03');
-> 'Saturday'Returns the day of the month for 对于date, in the range 1 to 31, or 0 for dates such as '0000-00-00' or '2008-00-00' that have a zero day part.date返回一个月中的某一天,范围为1到31,对于'0000-00-00'或'2008-00-00'等包含零天部分的日期返回0。
mysql> SELECT DAYOFMONTH('2007-02-03');
-> 3Returns the weekday index for 返回date (1 = Sunday, 2 = Monday, …, 7 = Saturday). date的星期几的索引(1=星期日,2=星期一,……,7=星期六)。These index values correspond to the ODBC standard.这些索引值对应于ODBC标准。
mysql> SELECT DAYOFWEEK('2007-02-03');
-> 7Returns the day of the year for 返回date, in the range 1 to 366.date的一年中的某一天,范围从1到366。
mysql> SELECT DAYOFYEAR('2007-02-03');
-> 34the EXTRACT() function uses the same kinds of unit specifiers as DATE_ADD() or DATE_SUB(), but extracts parts from the date rather than performing date arithmetic. EXTRACT()函数使用与DATE_ADD()或DATE_SUB()相同的单位说明符,但从日期中提取部分,而不是执行日期算术。For information on the 有关unit argument, see Temporal Intervals.unit参数的信息,请参见时间间隔。
mysql>SELECT EXTRACT(YEAR FROM '2019-07-02');-> 2019 mysql>SELECT EXTRACT(YEAR_MONTH FROM '2019-07-02 01:02:03');-> 201907 mysql>SELECT EXTRACT(DAY_MINUTE FROM '2019-07-02 01:02:03');-> 20102 mysql>SELECT EXTRACT(MICROSECOND->FROM '2003-01-02 10:30:00.000123');-> 123
Given a day number 给定一个日数N, returns a DATE value.N,返回一个DATE值。
mysql> SELECT FROM_DAYS(730669);
-> '2000-07-03'
Use 在旧日期使用FROM_DAYS() with caution on old dates. FROM_DAYS()时要小心。It is not intended for use with values that precede the advent of the Gregorian calendar (1582). 它不适用于公历(1582)出现之前的值。See Section 12.9, “What Calendar Is Used By MySQL?”.请参阅第12.9节,“MySQL使用什么日历?”。
FROM_UNIXTIME(unix_timestamp[,format])
Returns a representation of the 返回unix_timestamp argument as a value in ' or YYYY-MM-DD hh:mm:ss'YYYYMMDDhhmmss format, depending on whether the function is used in a string or numeric context. unix_timestamp参数的值表示形式,格式为'或YYYY-MM-DD hh:mm:ss'YYYYMMDDhhmmss,具体取决于函数是在字符串上下文中使用还是在数字上下文中使用。unix_timestamp is an internal timestamp value representing seconds since '1970-01-01 00:00:00' UTC, such as produced by the UNIX_TIMESTAMP() function.unix_timestamp是一个内部时间戳值,表示自'1970-01-01 00:00:00'UTC以来的秒数,例如由unix_timestamp()函数生成的秒数。
the return value is expressed in the session time zone. 返回值以会话时区表示。(Clients can set the session time zone as described in Section 5.1.15, “MySQL Server Time Zone Support”.) (客户端可以设置会话时区,如第5.1.15节,“MySQL服务器时区支持”所述。)the format string, if given, is used to format the result the same way as described in the entry for the DATE_FORMAT() function.format字符串(如果给定)用于格式化结果,方式与DATE_format()函数的条目中所述的相同。
mysql>SELECT FROM_UNIXTIME(1447430881);-> '2015-11-13 10:08:01' mysql>SELECT FROM_UNIXTIME(1447430881) + 0;-> 20151113100801 mysql>SELECT FROM_UNIXTIME(1447430881,->'%Y %D %M %h:%i:%s %x');-> '2015 13th November 10:08:01 2015'
If you use 如果使用UNIX_TIMESTAMP() and FROM_UNIXTIME() to convert between values in a non-UTC time zone and Unix timestamp values, the conversion is lossy because the mapping is not one-to-one in both directions. UNIX_TIMESTAMP()和FROM_UNIXTIME()在非UTC时区中的值和UNIX时间戳值之间进行转换,则转换是有损的,因为在两个方向上的映射不是一对一的。For details, see the description of the 有关详细信息,请参阅UNIX_TIMESTAMP() function.UNIX_TIMESTAMP()函数的说明。
GET_FORMAT({DATE|TIME|DATETIME}, {'EUR'|'USA'|'JIS'|'ISO'|'INTERNAL'})
Returns a format string. This function is useful in combination with the 返回格式字符串。此函数与DATE_FORMAT() and the STR_TO_DATE() functions.DATE_FORMAT()和STR_TO_DATE()函数结合使用非常有用。
the possible values for the first and second arguments result in several possible format strings (for the specifiers used, see the table in the 第一个和第二个参数的可能值会产生几个可能的格式字符串(有关使用的说明符,请参阅DATE_FORMAT() function description). DATE_FORMAT()函数说明中的表格)。ISO format refers to ISO 9075, not ISO 8601.ISO格式指的是ISO 9075,而不是ISO 8601。
GET_FORMAT(DATE,'USA') | '%m.%d.%Y' |
GET_FORMAT(DATE,'JIS') | '%Y-%m-%d' |
GET_FORMAT(DATE,'ISO') | '%Y-%m-%d' |
GET_FORMAT(DATE,'EUR') | '%d.%m.%Y' |
GET_FORMAT(DATE,'INTERNAL') | '%Y%m%d' |
GET_FORMAT(DATETIME,'USA') | '%Y-%m-%d %H.%i.%s' |
GET_FORMAT(DATETIME,'JIS') | '%Y-%m-%d %H:%i:%s' |
GET_FORMAT(DATETIME,'ISO') | '%Y-%m-%d %H:%i:%s' |
GET_FORMAT(DATETIME,'EUR') | '%Y-%m-%d %H.%i.%s' |
GET_FORMAT(DATETIME,'INTERNAL') | '%Y%m%d%H%i%s' |
GET_FORMAT(TIME,'USA') | '%h:%i:%s %p' |
GET_FORMAT(TIME,'JIS') | '%H:%i:%s' |
GET_FORMAT(TIME,'ISO') | '%H:%i:%s' |
GET_FORMAT(TIME,'EUR') | '%H.%i.%s' |
GET_FORMAT(TIME,'INTERNAL') | '%H%i%s' |
TIMESTAMP can also be used as the first argument to GET_FORMAT(), in which case the function returns the same values as for DATETIME.TIMESTAMP还可以用作GET_FORMAT()的第一个参数,在这种情况下,函数返回的值与DATETIME的值相同。
mysql>SELECT DATE_FORMAT('2003-10-03',GET_FORMAT(DATE,'EUR'));-> '03.10.2003' mysql>SELECT STR_TO_DATE('10.31.2003',GET_FORMAT(DATE,'USA'));-> '2003-10-31'
Returns the hour for 返回time. time的小时。the range of the return value is 对于时间值,返回值的范围是0 to 23 for time-of-day values. 0到23。However, the range of 但是,TIME values actually is much larger, so HOUR can return values greater than 23.TIME值的范围实际上要大得多,因此HOUR可以返回大于23的值。
mysql>SELECT HOUR('10:05:03');-> 10 mysql>SELECT HOUR('272:59:59');-> 272
Takes a date or datetime value and returns the corresponding value for the last day of the month. 获取日期或日期时间值并返回当月最后一天的相应值。Returns 如果参数无效,则返回NULL if the argument is invalid.NULL。
mysql>SELECT LAST_DAY('2003-02-05');-> '2003-02-28' mysql>SELECT LAST_DAY('2004-02-05');-> '2004-02-29' mysql>SELECT LAST_DAY('2004-01-01 01:01:01');-> '2004-01-31' mysql>SELECT LAST_DAY('2003-03-32');-> NULL
LOCALTIME and LOCALTIME() are synonyms for NOW().LOCALTIME和LOCALTIME()是NOW()的同义词。
LOCALTIMESTAMP, LOCALTIMESTAMP([fsp])
LOCALTIMESTAMP and LOCALTIMESTAMP() are synonyms for NOW().LOCALTIMESTAMP和LOCALTIMESTAMP()是NOW()的同义词。
Returns a date, given year and day-of-year values. 返回一个日期,给定年份和年份值。dayofyear must be greater than 0 or the result is NULL.dayofyear必须大于0或结果为NULL。
mysql>SELECT MAKEDATE(2011,31), MAKEDATE(2011,32);-> '2011-01-31', '2011-02-01' mysql>SELECT MAKEDATE(2011,365), MAKEDATE(2014,365);-> '2011-12-31', '2014-12-31' mysql>SELECT MAKEDATE(2011,0);-> NULL
Returns a time value calculated from the 返回从hour, minute, and second arguments.hour、minute和second参数计算的时间值。
the second argument can have a fractional part.second参数可以有小数部分。
mysql> SELECT MAKETIME(12,15,30);
-> '12:15:30'Returns the microseconds from the time or datetime expression 以expr as a number in the range from 0 to 999999.0到999999之间的数字形式返回时间或日期时间表达式expr的微秒数。
mysql>SELECT MICROSECOND('12:00:00.123456');-> 123456 mysql>SELECT MICROSECOND('2019-12-31 23:59:59.000010');-> 10
Returns the minute for 返回time, in the range 0 to 59.time的分钟,范围为0到59。
mysql> SELECT MINUTE('2008-02-03 10:05:03');
-> 5Returns the month for 返回date, in the range 1 to 12 for January to December, or 0 for dates such as '0000-00-00' or '2008-00-00' that have a zero month part.date的月份,1月到12月的范围在1到12之间,或0表示月份部分为零的日期,如'0000-00-00'或'2008-00-00'。
mysql> SELECT MONTH('2008-02-03');
-> 2Returns the full name of the month for 返回date. date的月份全名。the language used for the name is controlled by the value of the 用于名称的语言由lc_time_names system variable (Section 10.16, “MySQL Server Locale Support”).lc_time_names系统变量的值控制(第10.16节,“MySQL服务器区域设置支持”)。
mysql> SELECT MONTHNAME('2008-02-03');
-> 'February'Returns the current date and time as a value in 将当前日期和时间以' or YYYY-MM-DD hh:mm:ss'YYYYMMDDhhmmss format, depending on whether the function is used in string or numeric context. '格式或YYYY-MM-DD hh:mm:ss'YYYYMMDDhhmmss格式的值返回,具体取决于函数是在字符串上下文或数字上下文中使用。the value is expressed in the session time zone.该值以会话时区表示。
If the 如果给定fsp argument is given to specify a fractional seconds precision from 0 to 6, the return value includes a fractional seconds part of that many digits.fsp参数以指定0到6之间的分秒精度,则返回值将包含该多个数字中的分秒部分。
mysql>SELECT NOW();-> '2007-12-15 23:50:26' mysql>SELECT NOW() + 0;-> 20071215235026.000000
NOW() returns a constant time that indicates the time at which the statement began to execute. NOW()返回一个常量时间,指示语句开始执行的时间。(Within a stored function or trigger, (在存储的函数或触发器中,NOW() returns the time at which the function or triggering statement began to execute.) NOW()返回函数或触发器语句开始执行的时间。)This differs from the behavior for 这与SYSDATE(), which returns the exact time at which it executes.SYSDATE()的行为不同,后者返回它执行的确切时间。
mysql>SELECT NOW(), SLEEP(2), NOW();+---------------------+----------+---------------------+ | NOW() | SLEEP(2) | NOW() | +---------------------+----------+---------------------+ | 2006-04-12 13:47:36 | 0 | 2006-04-12 13:47:36 | +---------------------+----------+---------------------+ mysql>SELECT SYSDATE(), SLEEP(2), SYSDATE();+---------------------+----------+---------------------+ | SYSDATE() | SLEEP(2) | SYSDATE() | +---------------------+----------+---------------------+ | 2006-04-12 13:47:44 | 0 | 2006-04-12 13:47:46 | +---------------------+----------+---------------------+
In addition, the 此外,SET TIMESTAMP statement affects the value returned by NOW() but not by SYSDATE(). SET TIMESTAMP语句会影响NOW()返回的值,但不会影响SYSDATE()返回的值。This means that timestamp settings in the binary log have no effect on invocations of 这意味着二进制日志中的时间戳设置对SYSDATE(). SYSDATE()的调用没有影响。Setting the timestamp to a nonzero value causes each subsequent invocation of 将时间戳设置为非零值会导致随后每次调用NOW() to return that value. NOW()都返回该值。Setting the timestamp to zero cancels this effect so that 将时间戳设置为零将取消此效果,以便NOW() once again returns the current date and time.NOW()再次返回当前日期和时间。
See the description for 有关这两个函数之间差异的更多信息,请参见SYSDATE() for additional information about the differences between the two functions.SYSDATE()的说明。
Adds 将N months to period P (in the format YYMM or YYYYMM). N个月添加到时段P(格式为YYMM或YYYYMM)。Returns a value in the format 返回格式为YYYYMM.YYYYMM的值。
the period argument 时段参数P is not a date value.P不是日期值。
mysql> SELECT PERIOD_ADD(200801,2);
-> 200803Returns the number of months between periods 返回时段P1 and P2. P1和P2之间的月数。P1 and P2 should be in the format YYMM or YYYYMM. P1和P2的格式应为YYMM或YYYYMM。Note that the period arguments 注意,时段参数P1 and P2 are not date values.P1和P2不是日期值。
mysql> SELECT PERIOD_DIFF(200802,200703);
-> 11Returns the quarter of the year for 返回date, in the range 1 to 4.date的季度,范围为1到4。
mysql> SELECT QUARTER('2008-04-01');
-> 2Returns the second for 返回time, in the range 0 to 59.time的秒值,范围为0到59。
mysql> SELECT SECOND('10:05:03');
-> 3Returns the 返回seconds argument, converted to hours, minutes, and seconds, as a TIME value. seconds参数转换为小时、分钟和秒,作为TIME值。the range of the result is constrained to that of the 结果的范围限制为TIME data type. TIME数据类型的范围。A warning occurs if the argument corresponds to a value outside that range.如果参数对应的值超出该范围,则会出现警告。
mysql>SELECT SEC_TO_TIME(2378);-> '00:39:38' mysql>SELECT SEC_TO_TIME(2378) + 0;-> 3938
This is the inverse of the 这是DATE_FORMAT() function. It takes a string str and a format string format. DATE_FORMAT()函数的逆函数。它需要一个字符串str和一个格式字符串format。如果格式字符串同时包含日期和时间部分,STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts. STR_TO_DATE()将返回一个DATETIME值;如果格式字符串仅包含日期或时间部分,则返回一个DATE值或TIME值。If the date, time, or datetime value extracted from 如果从str is illegal, STR_TO_DATE() returns NULL and produces a warning.str中提取的日期、时间或日期时间值是非法的,str_TO_date()将返回NULL并产生警告。
the server scans 服务器扫描str attempting to match format to it. str,试图将格式与之匹配。the format string can contain literal characters and format specifiers beginning with 格式字符串可以包含以%. %开头的文字字符和格式说明符。Literal characters in format must match literally in str. format中的文字字符必须与str中的文字匹配。Format specifiers in format must match a date or time part in str. format中的格式说明符必须与str中的日期或时间部分匹配。For the specifiers that can be used in 有关可在format, see the DATE_FORMAT() function description.format中使用的说明符,请参阅DATE_format()函数说明。
mysql>SELECT STR_TO_DATE('01,5,2013','%d,%m,%Y');-> '2013-05-01' mysql>SELECT STR_TO_DATE('May 1, 2013','%M %d,%Y');-> '2013-05-01'
Scanning starts at the beginning of 扫描从str and fails if format is found not to match. str开头开始,如果发现格式不匹配,则扫描失败。Extra characters at the end of str are ignored.str结尾的额外字符将被忽略。
mysql>SELECT STR_TO_DATE('a09:30:17','a%h:%i:%s');-> '09:30:17' mysql>SELECT STR_TO_DATE('a09:30:17','%h:%i:%s');-> NULL mysql>SELECT STR_TO_DATE('09:30:17a','%h:%i:%s');-> '09:30:17'
Unspecified date or time parts have a value of 0, so incompletely specified values in 未指定的日期或时间部分的值为0,因此str produce a result with some or all parts set to 0:str中未完全指定的值将生成一个结果,其中部分或全部部分设置为0:
mysql>SELECT STR_TO_DATE('abc','abc');-> '0000-00-00' mysql>SELECT STR_TO_DATE('9','%m');-> '0000-09-00' mysql>SELECT STR_TO_DATE('9','%s');-> '00:00:09'
Range checking on the parts of date values is as described in Section 11.2.2, “the DATE, DATETIME, and TIMESTAMP Types”. 日期值部分的范围检查如第11.2.2节,“日期、日期时间和时间戳类型”所述。This means, for example, that “zero” dates or dates with part values of 0 are permitted unless the SQL mode is set to disallow such values.例如,这意味着允许“零”日期或部分值为0的日期,除非SQL模式设置为不允许这样的值。
mysql>SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');-> '0000-00-00' mysql>SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');-> '2004-04-31'
If the 如果启用了NO_ZERO_DATE SQL mode is enabled, zero dates are disallowed. NO_ZERO_DATESQL模式,则不允许零日期。In that case, 在这种情况下,STR_TO_DATE() returns NULL and generates a warning:STR_TO_DATE()返回NULL并生成警告:
mysql>SET sql_mode = '';mysql>SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');+---------------------------------------+ | STR_TO_DATE('00/00/0000', '%m/%d/%Y') | +---------------------------------------+ | 0000-00-00 | +---------------------------------------+ mysql>SET sql_mode = 'NO_ZERO_DATE';mysql>SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');+---------------------------------------+ | STR_TO_DATE('00/00/0000', '%m/%d/%Y') | +---------------------------------------+ | NULL | +---------------------------------------+ mysql>SHOW WARNINGS\G*************************** 1. row *************************** Level: Warning Code: 1411 Message: Incorrect datetime value: '00/00/0000' for function str_to_date
You cannot use format 不能使用格式"%X%V" to convert a year-week string to a date because the combination of a year and week does not uniquely identify a year and month if the week crosses a month boundary. “%X%V”将年-周字符串转换为日期,因为如果周跨越月边界,则年和周的组合不能唯一标识年和月。To convert a year-week to a date, you should also specify the weekday:要将年-周转换为日期,还应指定工作日:
mysql> SELECT STR_TO_DATE('200442 Monday', '%X%V %W');
-> '2004-10-18'SUBDATE(, date,INTERVAL expr unit)SUBDATE(expr,days)
When invoked with the 使用第二个参数的INTERVAL form of the second argument, SUBDATE() is a synonym for DATE_SUB(). INTERVAL形式调用时,SUBDATE()是DATE_SUB()的同义词。For information on the 有关INTERVAL unit argument, see the discussion for DATE_ADD().INTERVAL unit参数的信息,请参阅DATE_ADD()的讨论。
mysql>SELECT DATE_SUB('2008-01-02', INTERVAL 31 DAY);-> '2007-12-02' mysql>SELECT SUBDATE('2008-01-02', INTERVAL 31 DAY);-> '2007-12-02'
the second form enables the use of an integer value for 第二种形式允许对days. days使用整数值。In such cases, it is interpreted as the number of days to be subtracted from the date or datetime expression 在这种情况下,它被解释为从日期或日期时间表达式expr.expr中减去的天数。
mysql> SELECT SUBDATE('2008-01-02 12:00:00', 31);
-> '2007-12-02 12:00:00'SUBTIME() returns expr1 − expr2 expressed as a value in the same format as expr1. SUBTIME()返回expr1 − expr2,表示为与expr1格式相同的值。expr1 is a time or datetime expression, and expr2 is a time expression.expr1是时间或日期时间表达式,而expr2是时间表达式。
mysql>SELECT SUBTIME('2007-12-31 23:59:59.999999','1 1:1:1.000002');-> '2007-12-30 22:58:58.999997' mysql>SELECT SUBTIME('01:00:00.999999', '02:00:00.999998');-> '-00:59:59.999999'
Returns the current date and time as a value in 将当前日期和时间以' or YYYY-MM-DD hh:mm:ss'YYYYMMDDhhmmss format, depending on whether the function is used in string or numeric context.'或YYYY-MM-DD hh:mm:ss'YYYYMMDDhhmmss格式的值返回,具体取决于函数是在字符串上下文或数字上下文中使用。
If the 如果给定fsp argument is given to specify a fractional seconds precision from 0 to 6, the return value includes a fractional seconds part of that many digits.fsp参数以指定0到6之间的分秒精度,则返回值将包含该多个数字中的分秒部分。
SYSDATE() returns the time at which it executes. SYSDATE()返回它执行的时间。This differs from the behavior for 这与NOW(), which returns a constant time that indicates the time at which the statement began to execute. NOW()的行为不同,后者返回一个常量时间,指示语句开始执行的时间。(Within a stored function or trigger, (在存储的函数或触发器中,NOW() returns the time at which the function or triggering statement began to execute.)NOW()返回函数或触发器语句开始执行的时间。)
mysql>SELECT NOW(), SLEEP(2), NOW();+---------------------+----------+---------------------+ | NOW() | SLEEP(2) | NOW() | +---------------------+----------+---------------------+ | 2006-04-12 13:47:36 | 0 | 2006-04-12 13:47:36 | +---------------------+----------+---------------------+ mysql>SELECT SYSDATE(), SLEEP(2), SYSDATE();+---------------------+----------+---------------------+ | SYSDATE() | SLEEP(2) | SYSDATE() | +---------------------+----------+---------------------+ | 2006-04-12 13:47:44 | 0 | 2006-04-12 13:47:46 | +---------------------+----------+---------------------+
In addition, the 此外,SET TIMESTAMP statement affects the value returned by NOW() but not by SYSDATE(). SET TIMESTAMP语句会影响NOW()返回的值,但不会影响SYSDATE()返回的值。This means that timestamp settings in the binary log have no effect on invocations of 这意味着二进制日志中的时间戳设置对SYSDATE().SYSDATE()的调用没有影响。
Because 因为SYSDATE() can return different values even within the same statement, and is not affected by SET TIMESTAMP, it is nondeterministic and therefore unsafe for replication if statement-based binary logging is used. SYSDATE()即使在同一个语句中也可以返回不同的值,并且不受设置的时间戳的影响,所以如果使用基于语句的二进制日志记录,它是不确定的,因此对于复制是不安全的。If that is a problem, you can use row-based logging.如果这是一个问题,您可以使用基于行的日志记录。
Alternatively, you can use the 或者,可以使用--sysdate-is-now option to cause SYSDATE() to be an alias for NOW(). --sysdate-is-now选项使SYSDATE()成为NOW()的别名。This works if the option is used on both the replication source server and the replica.如果在复制源服务器和复制副本上都使用了该选项,则此操作有效。
the nondeterministic nature of SYSDATE() also means that indexes cannot be used for evaluating expressions that refer to it.SYSDATE()的不确定性还意味着索引不能用于计算引用它的表达式。
Extracts the time part of the time or datetime expression 提取时间或日期时间表达式expr and returns it as a string.expr的时间部分,并将其作为字符串返回。
This function is unsafe for statement-based replication. 此函数对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format is set to STATEMENT.binlog_format设置为STATEMENT时使用此函数,则会记录警告。
mysql>SELECT TIME('2003-12-31 01:02:03');-> '01:02:03' mysql>SELECT TIME('2003-12-31 01:02:03.000123');-> '01:02:03.000123'
TIMEDIFF() returns expr1 − expr2 expressed as a time value. TIMEDIFF()返回以时间值表示的expr1 − expr2。expr1 and expr2 are time or date-and-time expressions, but both must be of the same type.expr1和expr2是时间或日期和时间表达式,但两者必须是同一类型。
the result returned by TIMEDIFF() is limited to the range allowed for TIME values. TIMEDIFF()返回的结果限制在TIME值允许的范围内。Alternatively, you can use either of the functions 或者,可以使用函数TIMESTAMPDIFF() and UNIX_TIMESTAMP(), both of which return integers.TIMESTAMPDIFF()和UNIX_TIMESTAMP()中的任何一个,这两个函数都返回整数。
mysql>SELECT TIMEDIFF('2000:01:01 00:00:00',->'2000:01:01 00:00:00.000001');-> '-00:00:00.000001' mysql>SELECT TIMEDIFF('2008-12-31 23:59:59.000001',->'2008-12-30 01:01:01.000002');-> '46:58:57.999999'
TIMESTAMP(, expr)TIMESTAMP(expr1,expr2)
With a single argument, this function returns the date or datetime expression 对于单个参数,此函数将日期或日期时间表达式expr as a datetime value. 作为日期时间值返回。exprWith two arguments, it adds the time expression 使用两个参数,它将时间表达式expr2 to the date or datetime expression expr1 and returns the result as a datetime value.expr2添加到日期或日期时间表达式expr1,并将结果作为日期时间值返回。
mysql>SELECT TIMESTAMP('2003-12-31');-> '2003-12-31 00:00:00' mysql>SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');-> '2004-01-01 00:00:00'
TIMESTAMPADD(unit,interval,datetime_expr)
Adds the integer expression 将整数表达式interval to the date or datetime expression datetime_expr. interval添加到日期或日期时间表达式datetime_expr。the unit for interval is given by the unit argument, which should be one of the following values: MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.interval的单位由unit参数给出,它应该是以下值之一:MICROSECOND(微秒)、SECOND、MINUTE、HOUR、DAY、WEEK、MONTH、QUATER或YEAR。
the 可以使用如图所示的关键字之一指定unit value may be specified using one of keywords as shown, or with a prefix of SQL_TSI_. unit值,或者使用前缀SQL_TSI_指定unit值。For example, 例如,DAY and SQL_TSI_DAY both are legal.DAY和SQL_TSI_DAY都是合法的。
mysql>SELECT TIMESTAMPADD(MINUTE,1,'2003-01-02');-> '2003-01-02 00:01:00' mysql>SELECT TIMESTAMPADD(WEEK,1,'2003-01-02');-> '2003-01-09'
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
Returns 返回datetime_expr2 − datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. datetime_expr2 − datetime_expr1,其中datetime_expr1和datetime_expr2是日期或日期时间表达式。One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part 一个表达式可以是日期,另一个表达式可以是日期时间;如果需要,日期值将被视为时间部分为'00:00:00' where necessary. '00:00:00'的日期时间。the unit for the result (an integer) is given by the 结果的单位(整数)由unit argument. unit参数给出。the legal values for unit are the same as those listed in the description of the TIMESTAMPADD() function.unit的合法值与TIMESTAMPADD()函数描述中列出的值相同。
mysql>SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');-> 3 mysql>SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');-> -1 mysql>SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');-> 128885
the order of the date or datetime arguments for this function is the opposite of that used with the 此函数的日期或日期时间参数的顺序与使用2个参数调用TIMESTAMP() function when invoked with 2 arguments.TIMESTAMP()函数时使用的顺序相反。
This is used like the 这与DATE_FORMAT() function, but the format string may contain format specifiers only for hours, minutes, seconds, and microseconds. DATE_FORMAT()函数类似,但format字符串可能只包含小时、分钟、秒和微秒的格式说明符。Other specifiers produce a 其他说明符生成NULL value or 0.NULL值或0。
If the 如果time value contains an hour part that is greater than 23, the %H and %k hour format specifiers produce a value larger than the usual range of 0..23. time值包含大于23的小时部分,%H和%k小时格式说明符生成的值大于通常的范围0..23。the other hour format specifiers produce the hour value modulo 12.其他小时格式说明符生成小时值对12的取模。
mysql> SELECT TIME_FORMAT('100:00:00', '%H %k %h %I %l');
-> '100 100 04 04 4'Returns the 返回time argument, converted to seconds.time参数,转换为秒。
mysql>SELECT TIME_TO_SEC('22:23:00');-> 80580 mysql>SELECT TIME_TO_SEC('00:39:38');-> 2378
Given a date 给定一个date, returns a day number (the number of days since year 0).date,返回一个天数(从第0年开始的天数)。
mysql>SELECT TO_DAYS(950501);-> 728779 mysql>SELECT TO_DAYS('2007-10-07');-> 733321
TO_DAYS() is not intended for use with values that precede the advent of the Gregorian calendar (1582), because it does not take into account the days that were lost when the calendar was changed. TO_DAYS()不适用于公历(1582)出现之前的值,因为它不考虑更改日历时丢失的天数。For dates before 1582 (and possibly a later year in other locales), results from this function are not reliable. 对于1582年之前的日期(在其他地区可能是晚一年),此函数的结果不可靠。See Section 12.9, “What Calendar Is Used By MySQL?”, for details.有关详细信息,请参阅第12.9节,“MySQL使用什么日历?”。
Remember that MySQL converts two-digit year values in dates to four-digit form using the rules in Section 11.2, “Date and Time Data Types”. 请记住,MySQL使用第11.2节,“日期和时间数据类型”中的规则将日期中的两位数年份值转换为四位数形式。For example, 例如,'2008-10-07' and '08-10-07' are seen as identical dates:'2008-10-07'和'08-10-07'被视为相同的日期:
mysql> SELECT TO_DAYS('2008-10-07'), TO_DAYS('08-10-07');
-> 733687, 733687
In MySQL, the zero date is defined as 在MySQL中,零日期被定义为''0000-00-00'',即使这个日期本身被认为是无效的。'0000-00-00', even though this date is itself considered invalid. This means that, for 这意味着,对于'0000-00-00' and '0000-01-01', TO_DAYS() returns the values shown here:'0000-00-00'和'0000-01-01',TO_DAYS()返回如下所示的值:
mysql>SELECT TO_DAYS('0000-00-00');+-----------------------+ | to_days('0000-00-00') | +-----------------------+ | NULL | +-----------------------+ 1 row in set, 1 warning (0.00 sec) mysql>SHOW WARNINGS;+---------+------+----------------------------------------+ | Level | Code | Message | +---------+------+----------------------------------------+ | Warning | 1292 | Incorrect datetime value: '0000-00-00' | +---------+------+----------------------------------------+ 1 row in set (0.00 sec) mysql>SELECT TO_DAYS('0000-01-01');+-----------------------+ | to_days('0000-01-01') | +-----------------------+ | 1 | +-----------------------+ 1 row in set (0.00 sec)
This is true whether or not the 无论是否启用ALLOW_INVALID_DATES SQL server mode is enabled.ALLOW_INVALID_DATESSQL server模式,都是如此。
Given a date or datetime 给定日期或日期时间expr, returns the number of seconds since the year 0. expr,返回自0年以来的秒数。If 如果expr is not a valid date or datetime value, returns NULL.expr不是有效的日期或日期时间值,则返回NULL。
mysql>SELECT TO_SECONDS(950501);-> 62966505600 mysql>SELECT TO_SECONDS('2009-11-29');-> 63426672000 mysql>SELECT TO_SECONDS('2009-11-29 13:43:32');-> 63426721412 mysql>SELECT TO_SECONDS( NOW() );-> 63426721458
Like 与TO_DAYS(), TO_SECONDS() is not intended for use with values that precede the advent of the Gregorian calendar (1582), because it does not take into account the days that were lost when the calendar was changed. TO_DAYS()一样,TO_SECONDS()不适用于公历(1582)出现之前的值,因为它不考虑更改日历时丢失的天数。For dates before 1582 (and possibly a later year in other locales), results from this function are not reliable. 对于1582年之前的日期(在其他地区可能是晚一年),此函数的结果不可靠。See Section 12.9, “What Calendar Is Used By MySQL?”, for details.有关详细信息,请参阅第12.9节,“MySQL使用什么日历?”。
Like 与TO_DAYS(), TO_SECONDS(), converts two-digit year values in dates to four-digit form using the rules in Section 11.2, “Date and Time Data Types”.TO_DAYS()、TO_SECONDS()一样,使用第11.2节,“日期和时间数据类型”中的规则将日期中的两位数年份值转换为四位数形式。
In MySQL, the zero date is defined as 在MySQL中,零日期被定义为'0000-00-00', even though this date is itself considered invalid. '0000-00-00',即使这个日期本身被认为是无效的。This means that, for 这意味着,对于'0000-00-00' and '0000-01-01', TO_SECONDS() returns the values shown here:'0000-00-00'和'0000-01-01',TO_SECONDS()返回如下所示的值:
mysql>SELECT TO_SECONDS('0000-00-00');+--------------------------+ | TO_SECONDS('0000-00-00') | +--------------------------+ | NULL | +--------------------------+ 1 row in set, 1 warning (0.00 sec) mysql>SHOW WARNINGS;+---------+------+----------------------------------------+ | Level | Code | Message | +---------+------+----------------------------------------+ | Warning | 1292 | Incorrect datetime value: '0000-00-00' | +---------+------+----------------------------------------+ 1 row in set (0.00 sec) mysql>SELECT TO_SECONDS('0000-01-01');+--------------------------+ | TO_SECONDS('0000-01-01') | +--------------------------+ | 86400 | +--------------------------+ 1 row in set (0.00 sec)
This is true whether or not the 无论是否启用ALLOW_INVALID_DATES SQL server mode is enabled.ALLOW_INVALID_DATESSQL server模式,都是如此。
If 如果在调用UNIX_TIMESTAMP() is called with no date argument, it returns a Unix timestamp representing seconds since '1970-01-01 00:00:00' UTC.UNIX_TIMESTAMP()时不带date参数,它将返回一个UNIX时间戳,表示自'1970-01-01 00:00:00'UTC以来的秒数。
If 如果使用UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC. date参数调用UNIX_TIMESTAMP(),它将以秒的形式返回自'1970-01-01 00:00:00'UTC以来的参数值。the server interprets 服务器将date as a value in the session time zone and converts it to an internal Unix timestamp value in UTC. date解释为会话时区中的值,并将其转换为UTC格式的内部Unix时间戳值。(Clients can set the session time zone as described in Section 5.1.15, “MySQL Server Time Zone Support”.) (客户端可以设置会话时区,如第5.1.15节“MySQL服务器时区支持”所述。)the date argument may be a DATE, DATETIME, or TIMESTAMP string, or a number in YYMMDD, YYMMDDhhmmss, YYYYMMDD, or YYYYMMDDhhmmss format. date参数可以是DATE、DATETIME或TIMESTAMP字符串,也可以是YYMMDD、YYMMDDhhmmss、YYYYMMDD或YYYYMMDDhhmmss格式的数字。If the argument includes a time part, it may optionally include a fractional seconds part.如果参数包含时间部分,则可以选择包含小数秒部分。
the return value is an integer if no argument is given or the argument does not include a fractional seconds part, or 如果未给定参数或参数不包含小数秒部分,则返回值为整数;如果给定的参数包含小数秒部分,则返回值为DECIMAL if an argument is given that includes a fractional seconds part.DECIMAL。
When the 当date argument is a TIMESTAMP column, UNIX_TIMESTAMP() returns the internal timestamp value directly, with no implicit “string-to-Unix-timestamp” conversion.date参数是TIMESTAMP列时,UNIX_TIMESTAMP()直接返回内部时间戳值,没有隐式的“字符串到UNIX时间戳”转换。
the valid range of argument values is the same as for the 参数值的有效范围与TIMESTAMP data type: '1970-01-01 00:00:01.000000' UTC to '2038-01-19 03:14:07.999999' UTC. TIMESTAMP数据类型的有效范围相同:'1970-01-01 00:00:01.000000'UTC到'2038-01-19 03:14:07.999999'UTC。If you pass an out-of-range date to 如果将超出范围的日期传递给UNIX_TIMESTAMP(), it returns 0.UNIX_TIMESTAMP(),它将返回0。
mysql>SELECT UNIX_TIMESTAMP();-> 1447431666 mysql>SELECT UNIX_TIMESTAMP('2015-11-13 10:20:19');-> 1447431619 mysql>SELECT UNIX_TIMESTAMP('2015-11-13 10:20:19.012');-> 1447431619.012
If you use 如果使用UNIX_TIMESTAMP() and FROM_UNIXTIME() to convert between values in a non-UTC time zone and Unix timestamp values, the conversion is lossy because the mapping is not one-to-one in both directions. UNIX_TIMESTAMP()和FROM_UNIXTIME()在非UTC时区中的值和UNIX时间戳值之间进行转换,则转换是有损的,因为在两个方向上的映射不是一对一的。For example, due to conventions for local time zone changes such as Daylight Saving Time (DST), it is possible for 例如,由于本地时区更改(如夏令时)的约定,UNIX_TIMESTAMP() to map two values that are distinct in a non-UTC time zone to the same Unix timestamp value. UNIX_TIMESTAMP()可以将非UTC时区中不同的两个值映射到相同的UNIX时间戳值。FROM_UNIXTIME() maps that value back to only one of the original values. FROM_UNIXTIME()只将该值映射回一个原始值。Here is an example, using values that are distinct in the 下面是一个示例,使用MET time zone:MET时区中不同的值:
mysql>SET time_zone = 'MET';mysql>SELECT UNIX_TIMESTAMP('2005-03-27 03:00:00');+---------------------------------------+ | UNIX_TIMESTAMP('2005-03-27 03:00:00') | +---------------------------------------+ | 1111885200 | +---------------------------------------+ mysql>SELECT UNIX_TIMESTAMP('2005-03-27 02:00:00');+---------------------------------------+ | UNIX_TIMESTAMP('2005-03-27 02:00:00') | +---------------------------------------+ | 1111885200 | +---------------------------------------+ mysql>SELECT FROM_UNIXTIME(1111885200);+---------------------------+ | FROM_UNIXTIME(1111885200) | +---------------------------+ | 2005-03-27 03:00:00 | +---------------------------+
To use named time zones such as 要使用命名时区,如'MET' or 'Europe/Amsterdam', the time zone tables must be properly set up. 'MET'或'Europe/Amsterdam',必须正确设置时区表。For instructions, see Section 5.1.15, “MySQL Server Time Zone Support”.有关说明,请参阅第5.1.15节,“MySQL服务器时区支持”。
If you want to subtract 如果要减去UNIX_TIMESTAMP() columns, you might want to cast them to signed integers. UNIX_TIMESTAMP()列,可能需要将它们转换为有符号整数。See Section 12.11, “Cast Functions and Operators”.请参阅第12.11节,“强制转换函数和运算符”。
Returns the current UTC date as a value in 根据函数是在字符串上下文还是数字上下文中使用,以' or YYYY-MM-DD'YYYYMMDD format, depending on whether the function is used in string or numeric context.'或YYYY-MM-DD'YYYYMMDD格式返回当前UTC日期。
mysql> SELECT UTC_DATE(), UTC_DATE() + 0;
-> '2003-08-14', 20030814Returns the current UTC time as a value in 将当前UTC时间以'hh:mm:ss' or hhmmss format, depending on whether the function is used in string or numeric context.'hh:mm:ss'或hhmmss格式的值返回,具体取决于函数在字符串上下文或数字上下文中使用。
If the 如果给定fsp argument is given to specify a fractional seconds precision from 0 to 6, the return value includes a fractional seconds part of that many digits.fsp参数以指定0到6之间的分秒精度,则返回值将包含该多个数字中的分秒部分。
mysql> SELECT UTC_TIME(), UTC_TIME() + 0;
-> '18:07:53', 180753.000000UTC_TIMESTAMP, UTC_TIMESTAMP([fsp])
Returns the current UTC date and time as a value in 将当前UTC日期和时间以' or YYYY-MM-DD hh:mm:ss'YYYYMMDDhhmmss format, depending on whether the function is used in string or numeric context.'或YYYY-MM-DD hh:mm:ss'YYYYMMDDhhmmss格式的值返回,具体取决于函数是在字符串上下文还是数字上下文中使用。
If the 如果给定fsp argument is given to specify a fractional seconds precision from 0 to 6, the return value includes a fractional seconds part of that many digits.fsp参数以指定0到6之间的分秒精度,则返回值将包含该多个数字中的分秒部分。
mysql> SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0;
-> '2003-08-14 18:08:04', 20030814180804.000000This function returns the week number for 此函数返回date. date的周数。the two-argument form of WEEK() enables you to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53. WEEK()的双参数形式允许您指定星期是从星期日开始还是从星期一开始,以及返回值应在0到53之间还是1到53之间。If the 如果省略mode argument is omitted, the value of the default_week_format system variable is used. mode参数,则使用default_week_format系统变量的值。See Section 5.1.8, “Server System Variables”.请参阅第5.1.8节,“服务器系统变量”。
the following table describes how the 下表描述了mode argument works.mode参数的工作方式。
| 0 | Sunday | 0-53 | |
|---|---|---|---|
| 1 | Monday | 0-53 | |
| 2 | Sunday | 1-53 | |
| 3 | Monday | 1-53 | |
| 4 | Sunday | 0-53 | |
| 5 | Monday | 0-53 | |
| 6 | Sunday | 1-53 | |
| 7 | Monday | 1-53 |
For 对于具有“今年有4天或更多天”含义的mode values with a meaning of “with 4 or more days this year,” weeks are numbered according to ISO 8601:1988:mode值,根据ISO 8601:1988对周进行编号:
If the week containing January 1 has 4 or more days in the new year, it is week 1.如果包含1月1日的一周在新的一年中有4天或4天以上,则为第1周。
Otherwise, it is the last week of the previous year, and the next week is week 1.否则,这是上一年的最后一周,下一周是第1周。
mysql>SELECT WEEK('2008-02-20');-> 7 mysql>SELECT WEEK('2008-02-20',0);-> 7 mysql>SELECT WEEK('2008-02-20',1);-> 8 mysql>SELECT WEEK('2008-12-31',1);-> 53
If a date falls in the last week of the previous year, MySQL returns 如果日期落在上一年的最后一周,如果不使用0 if you do not use 2, 3, 6, or 7 as the optional mode argument:2、3、6或7作为可选模式参数,MySQL将返回0:
mysql> SELECT YEAR('2000-01-01'), WEEK('2000-01-01',0);
-> 2000, 0
One might argue that 有人可能认为WEEK() should return 52 because the given date actually occurs in the 52nd week of 1999. WEEK()应该返回52,因为给定的日期实际上发生在1999年的第52周。WEEK() returns 0 instead so that the return value is “the week number in the given year.” WEEK()返回0,因此返回值为“给定年份中的周数”。This makes use of the 这使得WEEK() function reliable when combined with other functions that extract a date part from a date.WEEK()函数与从日期中提取日期部分的其他函数结合使用时更加可靠。
If you prefer a result evaluated with respect to the year that contains the first day of the week for the given date, use 如果希望对包含给定日期每周第一天的年份计算结果,请使用0, 2, 5, or 7 as the optional mode argument.0、2、5或7作为可选模式参数。
mysql> SELECT WEEK('2000-01-01',2);
-> 52
Alternatively, use the 或者,使用YEARWEEK() function:YEARWEEK()函数:
mysql>SELECT YEARWEEK('2000-01-01');-> 199952 mysql>SELECT MID(YEARWEEK('2000-01-01'),5,2);-> '52'
Returns the weekday index for 返回datedate的星期几索引 (0 = Monday, 1 = Tuesday, … 6 = Sunday).
mysql>SELECT WEEKDAY('2008-02-03 22:23:00');-> 6 mysql>SELECT WEEKDAY('2007-11-06');-> 1
Returns the calendar week of the date as a number in the range from 以1 to 53. 1到53之间的数字返回日期的日历周。WEEKOFYEAR() is a compatibility function that is equivalent to WEEK(.date,3)WEEKOFYEAR()是一个兼容函数,相当于WEEK(。date,3)
mysql> SELECT WEEKOFYEAR('2008-02-20');
-> 8Returns the year for 返回date, in the range 1000 to 9999, or 0 for the “zero” date.date的年份,范围为1000到9999,或0表示“零”日期。
mysql> SELECT YEAR('1987-01-01');
-> 1987YEARWEEK(, date)YEARWEEK(date,mode)
Returns year and week for a date. 返回日期的年和周。the year in the result may be different from the year in the date argument for the first and the last week of the year.结果中的年份可能与一年中第一周和最后一周的日期参数中的年份不同。
the mode argument works exactly like the mode argument to WEEK(). mode参数的工作方式与WEEK()的mode参数完全相同。For the single-argument syntax, a 对于单参数语法,使用mode value of 0 is used. mode值0。Unlike 与WEEK(), the value of default_week_format does not influence YEARWEEK().WEEK()不同,default_week_format格式的值不影响YEARWEEK()。
mysql> SELECT YEARWEEK('1987-01-01');
-> 198652
the week number is different from what the 星期数与WEEK() function would return (0) for optional arguments 0 or 1, as WEEK() then returns the week in the context of the given year.week()函数为可选参数0或1返回的值(0)不同,因为week()随后返回给定年份上下文中的星期。