MySQL time date types and related functions

MySQL time date types and related functions

 

1. Date type

Date and time types in MySQL
Types of byte minimum maximum value zero value
DATETIME 8 1000-01-01 00:00:00 9999-12-31 23:59:59 0000-00-00 00:00:00
DATE 4 1000-01-01 9999-12-31 0000-00-00
TIMESTAMP 4 19700101080001 sometime in 2038 00000000000000
TIME 3 -838:59:59 838:59:59 00:00:00
YEAR 1 1901 2155 0000

 

The format returned by TIMESTAMP is "YYYY-MM-DD HH:MM:SS", and the width is fixed to 19 characters. To get the numeric value, you can select the current_timestamp+0 method:


 

 

YEAR has year in 2-digit (version before 5.5.27) or 4-digit format, the default is 4-digit format, in 4-digit format, allowed values ​​are 1901 ~ 2155 and 0000, in 2-digit format, allowed values It is 70 ~ 69, which means 1970 ~ 2069, and the format returned by YEAR is "YYYY".

 

For all the above time and date types, if you want to insert the value corresponding to the current time, you can use current_timestamp, now():


 
 

 

Second, time and date functions 

 

Datetime functions in MySQL
function Function Format
CURDATE () Returns the current date 2016-10-29
LIKE() return current time 11:20:34
NOW() Returns the current date and time 2016-10-29 11:21:02

UNIX_TIMESTAMP(date)

UNIX_TIMESTAMP()

Returns the UNIX timestamp of the date date

Execute: select unix_timestamp()

Output: 1477711822

FROM_UNIXTIME(unix_timestamp) Returns the date value of a UNIX timestamp

Execute: select from_unixtime(1477670400)

Output: 2016-10-29 11:30:22

WEEK(date) Returns date as the week of the year

Execute: select week(curdate())

output: 43

YEAR(date) Returns the year of the date date

Execute: select year(curdate())

Output: 2016

HOUR(time) Returns the hour value of time

Execute: select hour(curtime());

output: 11

MINUTE(time) Returns the minute value of time

Execute: select minute(curtime())

Output: 36

MONTHNAME(date) Returns the month name of date

Execute: select monthname(curdate())

outputOctober

DATE_FORMAT(date, format) Returns the date value formatted in string format

Execution: select date_format(now(), '%Y year %c month %e day %T hour %i minute %S second');

Output: Oct 29, 2016 11:44:57:44:57

DATE_ADD(date,INTERVAL expr type) Returns a date value plus the time value of a time interval

执行:select now() current,date_add(now(),INTERVAL 1 day) after_one_day;

Output: 2016-10-29 11:52:10 | 2016-10-30 11:52:10

DATEDIFF(expr1,expr2) Returns the number of days between start time expr1 and end time expr2

Execute: select datediff('2016-10-20',now());

output: -9

Execute: select datediff(now(),'2016-10-20')

output: 9

 

UNIX_TIMESTAMP(date) and FROM_UNIXTIME(unix_timestamp) are reciprocal.

 

DATE_FORMAT(date,format) function:

 

Format character in format string:

Date and time formats in MySQL
format character format description
%S and %s Seconds as two digits (00, 01, ... , 59)
%i Two-digit minute (00, 01, ... , 59)
%H Two-digit hour, 24 hours (00,01,...,23)

% h sum% I

Two-digit hour, 24 hours (01,02,...,12)
%k Hours as numbers, 24 hours (0,1,2,...,23)
%l Hours in numeric form, 12 hours (1,2,...,12)
%T 24-hour time format (hh:mm:ss)
%r 12-hour time format (hh:mm:ssAM or hh:mm:ssPM)
%p AM or PM
%W The name of each day of the week (Sunday,Monday,...,Saturday)
%a Abbreviated names for each day of the week (Sun,Mon,...,Sat)
%d Two digits representing the day of the month (00,01,...,31)
%e The number represents the day of the month (1,2,...,31)
%D The English suffix indicates the number of days in the month (1st, 2nd, 3rd,...)
%w Number of days of the week (0=Sunday, 1=Monday...)
%j Day of the year as 3 digits (001,002,...,366)
% U Week of the year (0,1...,52), where Sunday is the first day of the week
% u Week of the year (0,1...,52), where Monday is the first day of the week
%M Month name (January,February,...,December)
%b Abbreviated month names (Jan,Feb,...Dec)
%m Two-digit month (01,02,...,12)
%c Number of months (1,2,...,12)
%AND 4-digit year
%and year in two digits
%% escape

 

Example:


 

 

 

 DATE_ADD(date,INTERVAL expr type): The INTERVAL time interval type keyword, expr is an expression, corresponding to the following type, type time interval type, its value is as follows

MySQL date interval type
expression type describe Format
HOUR Hour hh
MINUTE Minute mm
SECOND Second ss
YEAR year YY

MONTH

moon MM
DAY day DD
YEAR_MONTH year and month YY-MM
DAY_HOUR day and hour DD hh
DAY_MINUTE 日和分钟 DD hh:mm
DAY_SECOND 日和秒 DD hh:mm:ss
HOUR_MINUTE 小时和分 hh:mm
HOUR_SECOND 小时和秒 hh:ss
MINUTE_SECOND 分钟和秒 mm:ss

 

例如,得到3天5小时后的时间:

 

 

得到3天5小时前的时间:

 

 

关于datetime和timestamp

 

MySQL中datetime能保存的日期范围从1001年到9999年,精度为秒,存储时把日期和时间封装为YYYYMMDDHHMMSS的格式的整数中,与时区无关,使用8个字节存储。

 

timestamp类型能保存的日期范围从1970年1月1日到2038年12月31日,它存储的是时间戳,只用了4个字节,所以范围比datetime小很多,timestamp是和时区相关的。默认情况下,如果插入时没有指定第一个timestamp列的值,那么mysql会默认给设置当前时间。在更新一行记录时,也会更新第一个timestamp列的值为当前时间(除非指定了该列的值)

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326585802&siteId=291194637