MySQL common date and time functions
date and time functions
Possible needs:
What is the current time, today is the day of the week in the next month, the total income of the 3 days before the current date is counted...
The above requirements need to be implemented using date and time functions:
There are three time zone settings in MySQL server:
①System time zone - saved in the system variable system_time_zone
②Server time zone - saved in the global system variable global.time_zone
③ The time zone of each client connection - saved in the session variable session.time_zone
Notice:
The setting of the client time zone will affect the display of the return value of some date functions, such as now(), curtime(), curdate(), and also the display of the timestamp column value.
By default, the time zone of the client and server is the same, and its value is SYSTEM, which means the system time zone is used.
复制代码
mysql> select @@global.time_zone,@@session.time_zone;
+——————–+———————+
| @@global.time_zone | @@session.time_zone |
+——————–+———————+
| SYSTEM | SYSTEM |
+——————–+———————+
1 row in set (0.00 sec)
mysql> show variables like ‘system_time_zone’;
+——————+——-+
| Variable_name | Value |
+——————+——-+
| system_time_zone | CST |
+——————+——-+
1 row in set (0.28 sec)
复制代码
1. NOW([fsp]): Returns the current date and time of the server (fsp specifies the precision of fractional seconds, with a value of 0–6)
Format:
'YYYY-MM-DD HH:MM:SS' or 'YYYYMMDDHHMMSS'
The display format of copy code
now() is 'YYYY-MM-DD HH:MM:SS'
The display format of now()+0 is 'YYYYMMDDHHMMSS'
mysql> select now();
+——————+
| now() |
+——————+
| 2017-03-24 13:53:34 |
+——————+
mysql> select now()+0;
+————-+
| now()+0 |
+————-+
| 20170324135428 |
+————-+
mysql> select now(6) ; //Specify the precision of fractional seconds
+—————————-+
| now(6) |
+—————————-+
| 2017-04-19 19:55:46.658198 |
+—————————-+
Synonyms for the now() function are: CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(), LOCALTIMESTAMP
, LOCALTIMESTAMP(), LOCALTIME, LOCALTIME()
Notice:
SYSDATE( ): Returns the current date and time of the server
The difference from now: (usually use NOW instead of SYSDATE)
①SYSDATE() returns the time when the function is executed
②now() returns the time when the statement was executed
复制代码
mysql> select now(),sleep(2),now();
+———————+———-+———————+
| now() | sleep(2) | now() |
+———————+———-+———————+
| 2017-04-19 20:01:39 | 0 | 2017-04-19 20:01:39 |
+———————+———-+———————+
1 row in set (2.00 sec)
mysql> select sysdate(),sleep(2),sysdate();
+———————+———-+———————+
| sysdate() | sleep(2) | sysdate() |
+———————+———-+———————+
| 2017-04-19 20:02:01 | 0 | 2017-04-19 20:02:03 |
+———————+———-+———————+
1 row in set (2.05 sec)
复制代码
2. CURTIME([fsp]): Returns the current time, including only hours, minutes and seconds (fsp specifies the precision of fractional seconds, with a value of 0–6)
Format:
'YYYY-MM-DD HH:MM:SS' or 'YYYYMMDDHHMMSS'
Copy code
mysql> select curtime(),curtime(2);
+——–+————-+
| curtime() | curtime(2) |
+——–+————-+
| 14 :35:23 | 14:35:23.90 |
+——–+————-+
Copy code
Synonyms are: CURRENT_TIME , CURRENT_TIME()
3. CURDATE(): Returns the current date, including only the year, month and day
Format:
'YYYY-MM-DD' or 'YYYYMMDD'
Copy code
mysql> select curdate(),curdate()+2;
+————+————-+
| curdate() | curdate()+2 |
+————+————-+
| 2017-03-24 | 20170326 |
+————+————-+
mysql> select curdate(),curdate()+0;
+————+————-+
| curdate() | curdate()+0 |
+————+————-+
| 2017 -03-24 | 20170324 |
+————+————-+
Copy code
Synonyms are: CURRENT_DATE, CURRENT_DATE()
4. TIMEDIFF(expr1, expr2): Returns the time difference between two dates (expr1 − expr2 ) (the two parameter types must be the same)
Copy code
mysql> select timediff('18:32:59','60000');
+————————+
| timediff('18:32:59','60000') |
+— —————————+
| 12:32:59 |
+—————————+
mysql> select timediff('18:32:59','2017-1-1 60000');
+————————————+
| timediff('18:32:59',' 2017-1-1 60000') |
+————————————+
| NULL |
+————————————+
Copy Code
DATEDIFF(expr1, expr2): Returns the number of days between two dates minus (expr1 − expr2 )
Copy code
mysql> select datediff('2017-3-24 18:32:59','2016-9-1');
+——————————————-+
| datediff(' 2017-3-24 18:32:59','2016-9-1') |
+——————————————-+
| 204 |
+———————— ——————-+
copy code
5. Date and time operation function: add (add) or subtract (sub) a time interval value expr to the given date respectively
Format:
DATE_ADD(date, INTERVAL expr unit);
DATE_SUB(date, INTERVAL expr unit);
interval is the interval type keyword
expr is an expression corresponding to the following type
unit is the unit (interval type) (20) of the time interval, as follows:
HOUR
Hour
MINUTE
Minute
SECOND
Second
MICROSECOND
millisecond
YEAR
year
MONTH
moon
DAY
day
WEEK
week
QUARTER
season
YEAR_MONTH
year and month
DAY_HOUR
day and hour
DAY_MINUTE
day and minute
DAY_ SECOND
day and second
HOUR_MINUTE
hours and minutes
HOUR_SECOND
hours and seconds
MINUTE_SECOND
minutes and seconds
Copy code
mysql> select now(),date_add(now(),interval 1 day); #Add one day
+——————+———————————–+
| now() | date_add (now(),interval 1 day) |
+——————+—————————–+
| 2017-03-24 14:53:08 | 2017-03-25 14: 53:08 |
+————————+———————————–+
mysql> SELECT date_sub('2005-01-01 00:00:00',INTERVAL '1 1:1:1' DAY_SECOND); #Subtract 1 day, 1 hour, 1 minute and 1 second
+————————— ————————————+
| date_sub('2005-01-01 00:00:00',INTERVAL '1 1:1:1' DAY_SECOND) |
+———————— ————————————+
| 2004-12-30 22:58:59 |
+—————————————————————+
Copy code
You can also write expressions to add and subtract dates without using functions:
date + INTERVAL expr unit
date - INTERVAL expr unit
Copy code
mysql> SELECT '2008-12-31 23:59:59' + INTERVAL 1 SECOND;
+———————————————-+
| '2008-12-31 23:59: 59' + INTERVAL 1 SECOND |
+———————————————-+
| 2009-01-01 00:00:00 |
+—————————————— —-+
1 row in set (0.00 sec)
mysql> SELECT '2005-01-01' - INTERVAL 1 SECOND;
+———————————-+
| '2005-01-01' - INTERVAL 1 SECOND |
+———————— ————-+
| 2004-12-31 23:59:59 |
+———————————-+
1 row in set (0.00 sec
)
6. Select each part of the date and time: date, time, year, quarter, month, day, hour, minute, second, microsecond (commonly used)
SELECT now(),date(now()); – 日期
SELECT now(),time(now()); - time
SELECT now(),year(now()); – 年
SELECT now(),quarter(now()); – quarter
SELECT now(),month(now()); – 月
SELECT now(),week(now()); – 周
SELECT now(),day(now()); – 日
SELECT now(),hour(now()); – 小时
SELECT now(),minute(now()); – 分钟
SELECT now(),second(now()); – 秒
SELECT now(),microsecond(now()); – 微秒
EXTRACT (unit FROM date): extract a single part or combination from a date
SELECT now(),extract(YEAR FROM now()); – 年
SELECT now(),extract(QUARTER FROM now()); – 季度
SELECT now(),extract(MONTH FROM now()); – 月
SELECT now(),extract(WEEK FROM now()); – 周
SELECT now(),extract(DAY FROM now()); – 日
SELECT now(),extract(HOUR FROM now()); – 小时
SELECT now(),extract(MINUTE FROM now()); – 分钟
SELECT now(),extract(SECOND FROM now()); – 秒
SELECT now(),extract(YEAR_MONTH FROM now()); – 年月
SELECT now(),extract(HOUR_MINUTE FROM now()); – 时分
7. Personalized display time and date
dayofweek(date)
dayofmonth(date)
dayofyear(date)
Returns the day of the week, January, and year, respectively
复制代码
mysql> SELECT now(),dayofweek(now());
+———————+——————+
| now() | dayofweek(now()) |
+———————+——————+
| 2017-04-19 20:25:41 | 4 |
+———————+——————+
1 row in set (0.00 sec)
mysql> SELECT now(),dayofmonth(now());
+———————+——————-+
| now() | dayofmonth(now()) |
+———————+——————-+
| 2017-04-19 20:25:51 | 19 |
+———————+——————-+
1 row in set (0.03 sec)
mysql> select now(),dayofyear(now());
+———————+——————+
| now() | dayofyear(now()) |
+———————+——————+
| 2017-04-19 20:26:00 | 109 |
+———————+——————+
1 row in set (0.00 sec)
复制代码
dayname()
monthname()
Returns the week and month names of the date respectively
The name is Chinese or English controlled by the system variable lc_time_names (the default value is 'en_US')
复制代码
mysql> show variables like ‘lc_time_names’;
+—————+——-+
| Variable_name | Value |
+—————+——-+
| lc_time_names | en_US |
+—————+——-+
1 row in set (0.00 sec)
mysql> select dayname(now()),monthname(now());
+—————-+——————+
| dayname(now()) | monthname(now()) |
+—————-+——————+
| Wednesday | April |
+—————-+——————+
1 row in set (0.00 sec)
mysql> set lc_time_names=’zh_CN’;
Query OK, 0 rows affected (0.00 sec)
mysql> select dayname(now()),monthname(now());
+————-+————+
| dayname(now()) | monthname(now()) |
+—— ———-+——————+
|Wednesday|April|
+——————-+——————+
1 row in set (0.00 sec)
Copy Code