6.3 设置客户端时区
--客户端位于不同时区需要注意,如果位于同一时区则不需要关心
mysql> drop table if exists t;
Query OK, 0 rows affected (0.06 sec)mysql> create table t (ts timestamp);
Query OK, 0 rows affected (0.05 sec)mysql> insert into t (ts) values('2006-06-01 12:30:00');
Query OK, 1 row affected (0.01 sec)mysql> select ts from t;
+---------------------+
| ts |
+---------------------+
| 2006-06-01 12:30:00 |
+---------------------+
1 row in set (0.00 sec)Windows 中类似date命令的用法
如果需要查看日期时间,可在 CMD 中输入:
cmd
date /t # 仅显示日期(如 2025年06月10日)
time /t # 仅显示时间(如 14:30)mysql> SET SESSION time_zone = '+06:00';
Query OK, 0 rows affected (0.00 sec)mysql> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM | +06:00 |
+--------------------+---------------------+
1 row in set (0.00 sec)mysql> select ts from t;
+---------------------+
| ts |
+---------------------+
| 2006-06-01 10:30:00 |
+---------------------+
1 row in set (0.00 sec)mysql> set session time_zone = '+04:00';
Query OK, 0 rows affected (0.00 sec)mysql> select @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM | +04:00 |
+--------------------+---------------------+
1 row in set (0.00 sec)mysql> select ts from t;
+---------------------+
| ts |
+---------------------+
| 2006-06-01 08:30:00 |
+---------------------+
1 row in set (0.00 sec)
6.4 获取当前日期或时间
mysql> select curdate(), curtime(), now();
+------------+-----------+---------------------+
| curdate() | curtime() | now() |
+------------+-----------+---------------------+
| 2025-06-10 | 15:43:19 | 2025-06-10 15:43:19 |
+------------+-----------+---------------------+
1 row in set (0.00 sec)mysql> select current_date(), current_time(), current_timestamp();
+----------------+----------------+---------------------+
| current_date() | current_time() | current_timestamp() |
+----------------+----------------+---------------------+
| 2025-06-10 | 15:43:44 | 2025-06-10 15:43:44 |
+----------------+----------------+---------------------+
1 row in set (0.00 sec)mysql> select utc_date(), utc_time(), utc_timestamp();
+------------+------------+---------------------+
| utc_date() | utc_time() | utc_timestamp() |
+------------+------------+---------------------+
| 2025-06-10 | 11:44:10 | 2025-06-10 11:44:10 |
+------------+------------+---------------------+
1 row in set (0.00 sec)
6.5 使用timestamp来跟踪行修改时间
--timestamp初始化为当前日期,并随行值改变而改变
mysql> drop table if exists t;
Query OK, 0 rows affected (0.04 sec)mysql> create table t(ts timestamp);
Query OK, 0 rows affected (0.05 sec)mysql> show create table t\G
*************************** 1. row ***************************Table: t
Create Table: CREATE TABLE `t` (`ts` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)mysql> drop table if exists tsdemo1;
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> create table tsdemo1(ts timestamp, val int);
Query OK, 0 rows affected (0.05 sec)mysql> insert into tsdemo1(val) values(5);
Query OK, 1 row affected (0.01 sec)mysql> insert into tsdemo1(ts, val) values(null, 10);
Query OK, 1 row affected (0.01 sec)mysql> select * from tsdemo1;
+------+------+
| ts | val |
+------+------+
| NULL | 5 |
| NULL | 10 |
+------+------+
2 rows in set (0.00 sec)mysql> update tsdemo1 set val=6 where val=5;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from tsdemo1;
+------+------+
| ts | val |
+------+------+
| NULL | 6 |
| NULL | 10 |
+------+------+
2 rows in set (0.00 sec)mysql> update tsdemo1 set val= val+1;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0--timestamp初始化为当前日期,行值改变不改变
mysql> drop table if exists tsdemo2;
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> create table tsdemo2(t_create timestamp default current_timestamp, val int);
Query OK, 0 rows affected (0.06 sec)mysql> select * from tsdemo2;
Empty set (0.00 sec)mysql> insert into tsdemo2(val) values(5);
Query OK, 1 row affected (0.01 sec)mysql> insert into tsdemo2(t_create, val) values(null, 10);
Query OK, 1 row affected (0.01 sec)mysql> select * from tsdemo2;
+---------------------+------+
| t_create | val |
+---------------------+------+
| 2025-06-10 15:53:44 | 5 |
| NULL | 10 |
+---------------------+------+
2 rows in set (0.00 sec)mysql> update tsdemo2 set val = val+1;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0mysql> select * from tsdemo2;
+---------------------+------+
| t_create | val |
+---------------------+------+
| 2025-06-10 15:53:44 | 6 |
| NULL | 11 |
+---------------------+------+
2 rows in set (0.00 sec)
6.6 从日期或者时间值中分解出各部分值
--使用成分分解函数来分解日期和时间值
mysql> select dt, date(dt), time(dt) from datetime_val;
+---------------------+------------+----------+
| dt | date(dt) | time(dt) |
+---------------------+------------+----------+
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15 |
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15 |
+---------------------+------------+----------+
8 rows in set (0.01 sec)--日期相关函数
函数 返回值
year() 月份数值
month() 月份名称(1..12)
monthname() 月份中的天数值(January..December)
dayofmonth() 一周中的天数(1..31)
dayname() 一周中的天数(Sunday..Saturday)
dayofweek() 一周中的天数(1..7对应Sunday..Saturday)
weekday() 一周中的天数(0..6对应Monday..Sunday)
dayofyear() 一年中的天数值(1.366)
hour() 时间中的小数值(0..23)
minute() 时间中的分数值(0..59)
second() 时间中的秒数(0..59)mysql> select dt,-> year(dt), dayofmonth(dt),-> hour(dt), second(dt)-> from datetime_val;
+---------------------+----------+----------------+----------+------------+
| dt | year(dt) | dayofmonth(dt) | hour(dt) | second(dt) |
+---------------------+----------+----------------+----------+------------+
| 1970-01-01 00:00:00 | 1970 | 1 | 0 | 0 |
| 1999-12-31 09:00:00 | 1999 | 31 | 9 | 0 |
| 2000-06-04 15:45:30 | 2000 | 4 | 15 | 30 |
| 2017-03-16 12:30:15 | 2017 | 16 | 12 | 15 |
| 1970-01-01 00:00:00 | 1970 | 1 | 0 | 0 |
| 1999-12-31 09:00:00 | 1999 | 31 | 9 | 0 |
| 2000-06-04 15:45:30 | 2000 | 4 | 15 | 30 |
| 2017-03-16 12:30:15 | 2017 | 16 | 12 | 15 |
+---------------------+----------+----------------+----------+------------+
8 rows in set (0.01 sec)mysql> select d, dayofyear(d) from date_val;
+------------+--------------+
| d | dayofyear(d) |
+------------+--------------+
| 1864-02-28 | 59 |
| 1900-01-15 | 15 |
| 1999-12-31 | 365 |
| 2000-06-04 | 156 |
| 2017-03-16 | 75 |
| 1864-02-28 | 59 |
| 1900-01-15 | 15 |
| 1999-12-31 | 365 |
| 2000-06-04 | 156 |
| 2017-03-16 | 75 |
| 2007-05-13 | 133 |
+------------+--------------+
11 rows in set (0.00 sec)mysql> select d, dayname(d),left(dayname(d), 3) from date_val;
+------------+------------+---------------------+
| d | dayname(d) | left(dayname(d), 3) |
+------------+------------+---------------------+
| 1864-02-28 | Sunday | Sun |
| 1900-01-15 | Monday | Mon |
| 1999-12-31 | Friday | Fri |
| 2000-06-04 | Sunday | Sun |
| 2017-03-16 | Thursday | Thu |
| 1864-02-28 | Sunday | Sun |
| 1900-01-15 | Monday | Mon |
| 1999-12-31 | Friday | Fri |
| 2000-06-04 | Sunday | Sun |
| 2017-03-16 | Thursday | Thu |
| 2007-05-13 | Sunday | Sun |
+------------+------------+---------------------+
11 rows in set (0.00 sec)mysql> select d, dayname(d),dayofweek(d), weekday(d) from date_val;
+------------+------------+--------------+------------+
| d | dayname(d) | dayofweek(d) | weekday(d) |
+------------+------------+--------------+------------+
| 1864-02-28 | Sunday | 1 | 6 |
| 1900-01-15 | Monday | 2 | 0 |
| 1999-12-31 | Friday | 6 | 4 |
| 2000-06-04 | Sunday | 1 | 6 |
| 2017-03-16 | Thursday | 5 | 3 |
| 1864-02-28 | Sunday | 1 | 6 |
| 1900-01-15 | Monday | 2 | 0 |
| 1999-12-31 | Friday | 6 | 4 |
| 2000-06-04 | Sunday | 1 | 6 |
| 2017-03-16 | Thursday | 5 | 3 |
| 2007-05-13 | Sunday | 1 | 6 |
+------------+------------+--------------+------------+
11 rows in set (0.00 sec)mysql> select dt, extract(day from dt), extract(hour from dt) from datetime_val;
+---------------------+----------------------+-----------------------+
| dt | extract(day from dt) | extract(hour from dt) |
+---------------------+----------------------+-----------------------+
| 1970-01-01 00:00:00 | 1 | 0 |
| 1999-12-31 09:00:00 | 31 | 9 |
| 2000-06-04 15:45:30 | 4 | 15 |
| 2017-03-16 12:30:15 | 16 | 12 |
| 1970-01-01 00:00:00 | 1 | 0 |
| 1999-12-31 09:00:00 | 31 | 9 |
| 2000-06-04 15:45:30 | 4 | 15 |
| 2017-03-16 12:30:15 | 16 | 12 |
+---------------------+----------------------+-----------------------+
8 rows in set (0.00 sec)mysql> select curdate(), year(curdate()) as year, month(curdate()) as month,-> monthname(curdate()) as monthnaem, dayofmonth(curdate()) as day,-> dayname(curdate()) as dayname;
+------------+------+-------+-----------+------+---------+
| curdate() | year | month | monthnaem | day | dayname |
+------------+------+-------+-----------+------+---------+
| 2025-06-10 | 2025 | 6 | June | 10 | Tuesday |
+------------+------+-------+-----------+------+---------+
1 row in set (0.00 sec)mysql> select now(), hour(now()) as hour, minute(now()) as minute, second(now()) as second;
+---------------------+------+--------+--------+
| now() | hour | minute | second |
+---------------------+------+--------+--------+
| 2025-06-10 16:06:16 | 16 | 6 | 16 |
+---------------------+------+--------+--------+
1 row in set (0.00 sec)--使用格式化分解日期或者时间值
mysql> select dt, date_format(dt, '%Y') as year, date_format(dt, '%d') as day, time_format(dt, '%H') as hour, time_format(dt, '%s') as second from datetime_val;
+---------------------+------+------+------+--------+
| dt | year | day | hour | second |
+---------------------+------+------+------+--------+
| 1970-01-01 00:00:00 | 1970 | 01 | 00 | 00 |
| 1999-12-31 09:00:00 | 1999 | 31 | 09 | 00 |
| 2000-06-04 15:45:30 | 2000 | 04 | 15 | 30 |
| 2017-03-16 12:30:15 | 2017 | 16 | 12 | 15 |
| 1970-01-01 00:00:00 | 1970 | 01 | 00 | 00 |
| 1999-12-31 09:00:00 | 1999 | 31 | 09 | 00 |
| 2000-06-04 15:45:30 | 2000 | 04 | 15 | 30 |
| 2017-03-16 12:30:15 | 2017 | 16 | 12 | 15 |
+---------------------+------+------+------+--------+
8 rows in set (0.00 sec)mysql> select dt, date_format(dt, '%Y-%m-%d') as ' date part', time_format(dt, '%T') as' time part' from datetime_val;
+---------------------+------------+-----------+
| dt | date part | time part |
+---------------------+------------+-----------+
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15 |
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15 |
+---------------------+------------+-----------+
8 rows in set, 2 warnings (0.00 sec)mysql> select ts, date_format(ts, '%M %e %Y') as 'descriptive date', time_format(ts, '%H:%i') as'hours/minutes' from timestamp_val;
+---------------------+------------------+---------------+
| ts | descriptive date | hours/minutes |
+---------------------+------------------+---------------+
| 1970-01-01 05:00:00 | January 1 1970 | 05:00 |
| 1987-03-05 08:30:15 | March 5 1987 | 08:30 |
| 1999-12-31 05:00:00 | December 31 1999 | 05:00 |
| 2000-06-04 11:45:30 | June 4 2000 | 11:45 |
+---------------------+------------------+---------------+
4 rows in set (0.00 sec)--使用字符串函数分解时间或者日期值
mysql> select dt, left(dt, 4) as year, mid(dt, 9, 2) as day, right(dt, 2) as second from datetime_val;
+---------------------+------+------+--------+
| dt | year | day | second |
+---------------------+------+------+--------+
| 1970-01-01 00:00:00 | 1970 | 01 | 00 |
| 1999-12-31 09:00:00 | 1999 | 31 | 00 |
| 2000-06-04 15:45:30 | 2000 | 04 | 30 |
| 2017-03-16 12:30:15 | 2017 | 16 | 15 |
| 1970-01-01 00:00:00 | 1970 | 01 | 00 |
| 1999-12-31 09:00:00 | 1999 | 31 | 00 |
| 2000-06-04 15:45:30 | 2000 | 04 | 30 |
| 2017-03-16 12:30:15 | 2017 | 16 | 15 |
+---------------------+------+------+--------+
8 rows in set (0.00 sec)mysql> select dt, left(dt, 10) as date, right(dt, 8) as time from datetime_val;
+---------------------+------------+----------+
| dt | date | time |
+---------------------+------------+----------+
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15 |
| 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 |
| 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 |
| 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 |
| 2017-03-16 12:30:15 | 2017-03-16 | 12:30:15 |
+---------------------+------------+----------+
8 rows in set (0.00 sec)mysql> select ts, left(ts, 10) as date, right(ts, 8) as time from timestamp_val;
+---------------------+------------+----------+
| ts | date | time |
+---------------------+------------+----------+
| 1970-01-01 05:00:00 | 1970-01-01 | 05:00:00 |
| 1987-03-05 08:30:15 | 1987-03-05 | 08:30:15 |
| 1999-12-31 05:00:00 | 1999-12-31 | 05:00:00 |
| 2000-06-04 11:45:30 | 2000-06-04 | 11:45:30 |
+---------------------+------------+----------+
4 rows in set (0.00 sec)
6.7 合成日期或者时间值
mysql> select maketime(10,30,58), maketime(-5,0,11);
+--------------------+-------------------+
| maketime(10,30,58) | maketime(-5,0,11) |
+--------------------+-------------------+
| 10:30:58 | -05:00:11 |
+--------------------+-------------------+
1 row in set (0.00 sec)mysql> select makedate(2007, 60);
+--------------------+
| makedate(2007, 60) |
+--------------------+
| 2007-03-01 |
+--------------------+
1 row in set (0.00 sec)mysql> select d, date_format(d, '%Y-%m-01') from date_val;
+------------+----------------------------+
| d | date_format(d, '%Y-%m-01') |
+------------+----------------------------+
| 1864-02-28 | 1864-02-01 |
| 1900-01-15 | 1900-01-01 |
| 1999-12-31 | 1999-12-01 |
| 2000-06-04 | 2000-06-01 |
| 2017-03-16 | 2017-03-01 |
| 1864-02-28 | 1864-02-01 |
| 1900-01-15 | 1900-01-01 |
| 1999-12-31 | 1999-12-01 |
| 2000-06-04 | 2000-06-01 |
| 2017-03-16 | 2017-03-01 |
| 2007-05-13 | 2007-05-01 |
+------------+----------------------------+
11 rows in set (0.00 sec)mysql> select t1, time_format(t1, '%H:%i:00' ) from time_val;
+----------+------------------------------+
| t1 | time_format(t1, '%H:%i:00' ) |
+----------+------------------------------+
| 15:00:00 | 15:00:00 |
| 05:01:30 | 05:01:00 |
| 12:30:20 | 12:30:00 |
+----------+------------------------------+
3 rows in set (0.00 sec)mysql> select d, concat(year(d),'-', month(d), '-01') from date_val;
+------------+--------------------------------------+
| d | concat(year(d),'-', month(d), '-01') |
+------------+--------------------------------------+
| 1864-02-28 | 1864-2-01 |
| 1900-01-15 | 1900-1-01 |
| 1999-12-31 | 1999-12-01 |
| 2000-06-04 | 2000-6-01 |
| 2017-03-16 | 2017-3-01 |
| 1864-02-28 | 1864-2-01 |
| 1900-01-15 | 1900-1-01 |
| 1999-12-31 | 1999-12-01 |
| 2000-06-04 | 2000-6-01 |
| 2017-03-16 | 2017-3-01 |
| 2007-05-13 | 2007-5-01 |
+------------+--------------------------------------+
11 rows in set (0.00 sec)mysql> select d, concat(year(d), '-', lpad(month(d), 2, '0'), '-01') from date_val;
+------------+-----------------------------------------------------+
| d | concat(year(d), '-', lpad(month(d), 2, '0'), '-01') |
+------------+-----------------------------------------------------+
| 1864-02-28 | 1864-02-01 |
| 1900-01-15 | 1900-01-01 |
| 1999-12-31 | 1999-12-01 |
| 2000-06-04 | 2000-06-01 |
| 2017-03-16 | 2017-03-01 |
| 1864-02-28 | 1864-02-01 |
| 1900-01-15 | 1900-01-01 |
| 1999-12-31 | 1999-12-01 |
| 2000-06-04 | 2000-06-01 |
| 2017-03-16 | 2017-03-01 |
| 2007-05-13 | 2007-05-01 |
+------------+-----------------------------------------------------+
11 rows in set (0.00 sec)mysql> select d, concat(year(d), '-', lpad(month(d), 4, '0'), '-01') from date_val;
+------------+-----------------------------------------------------+
| d | concat(year(d), '-', lpad(month(d), 4, '0'), '-01') |
+------------+-----------------------------------------------------+
| 1864-02-28 | 1864-0002-01 |
| 1900-01-15 | 1900-0001-01 |
| 1999-12-31 | 1999-0012-01 |
| 2000-06-04 | 2000-0006-01 |
| 2017-03-16 | 2017-0003-01 |
| 1864-02-28 | 1864-0002-01 |
| 1900-01-15 | 1900-0001-01 |
| 1999-12-31 | 1999-0012-01 |
| 2000-06-04 | 2000-0006-01 |
| 2017-03-16 | 2017-0003-01 |
| 2007-05-13 | 2007-0005-01 |
+------------+-----------------------------------------------------+
11 rows in set (0.00 sec)mysql> select t1, concat(lpad(hour(t1), 2, '0'), ':', lpad(minute(t1), 2, '0'), ':00') as recombined from time_val;
+----------+------------+
| t1 | recombined |
+----------+------------+
| 15:00:00 | 15:00:00 |
| 05:01:30 | 05:01:00 |
| 12:30:20 | 12:30:00 |
+----------+------------+
3 rows in set (0.00 sec)mysql> set @d= '2006-02-28';
Query OK, 0 rows affected (0.00 sec)mysql> set @t='13:10:05';
Query OK, 0 rows affected (0.00 sec)mysql> SELECT @d, @t, CONCAT(@d, ' ', @t) AS datetime_combined;
+------------+----------+---------------------+
| @d | @t | datetime_combined |
+------------+----------+---------------------+
| 2006-02-28 | 13:10:05 | 2006-02-28 13:10:05 |
+------------+----------+---------------------+
1 row in set (0.00 sec)mysql> SELECT CONCAT_WS(' ', @d, @t);
+------------------------+
| CONCAT_WS(' ', @d, @t) |
+------------------------+
| 2006-02-28 13:10:05 |
+------------------------+
1 row in set (0.00 sec)
6.8 在时间数据类型和基本单位间进行转换
--在时间和秒之间进行转换
mysql> select t1, time_to_sec(t1) as 'TIME to seconds', sec_to_time(time_to_sec(t1)) as 'TIME to seconds to TIME' from time_val;
+----------+-----------------+-------------------------+
| t1 | TIME to seconds | TIME to seconds to TIME |
+----------+-----------------+-------------------------+
| 15:00:00 | 54000 | 15:00:00 |
| 05:01:30 | 18090 | 05:01:30 |
| 12:30:20 | 45020 | 12:30:20 |
+----------+-----------------+-------------------------+
3 rows in set (0.00 sec)mysql> select t1, time_to_sec(t1) as 'seconds', time_to_sec(t1)/60 as 'minutes', time_to_sec(t1)/(60*60) as hours, time_to_sec(t1)/(24*60*60) as 'days' from time_val;
+----------+---------+----------+---------+--------+
| t1 | seconds | minutes | hours | days |
+----------+---------+----------+---------+--------+
| 15:00:00 | 54000 | 900.0000 | 15.0000 | 0.6250 |
| 05:01:30 | 18090 | 301.5000 | 5.0250 | 0.2094 |
| 12:30:20 | 45020 | 750.3333 | 12.5056 | 0.5211 |
+----------+---------+----------+---------+--------+
3 rows in set (0.00 sec)mysql> select t1, time_to_sec(t1) as 'seconds',-> floor(time_to_sec(t1)/60) as 'minutes',-> floor(time_to_sec(t1)/(60*60)) as hours,-> floor(time_to_sec(t1)/(24*60*60)) as 'days'-> from time_val;
+----------+---------+---------+-------+------+
| t1 | seconds | minutes | hours | days |
+----------+---------+---------+-------+------+
| 15:00:00 | 54000 | 900 | 15 | 0 |
| 05:01:30 | 18090 | 301 | 5 | 0 |
| 12:30:20 | 45020 | 750 | 12 | 0 |
+----------+---------+---------+-------+------+
3 rows in set (0.00 sec)mysql> select dt, time_to_sec(dt) as 'time part in seconds',-> sec_to_time(time_to_sec(dt)) as 'time part as TIME'-> from datetime_val;
+---------------------+----------------------+-------------------+
| dt | time part in seconds | time part as TIME |
+---------------------+----------------------+-------------------+
| 1970-01-01 00:00:00 | 0 | 00:00:00 |
| 1999-12-31 09:00:00 | 32400 | 09:00:00 |
| 2000-06-04 15:45:30 | 56730 | 15:45:30 |
| 2017-03-16 12:30:15 | 45015 | 12:30:15 |
| 1970-01-01 00:00:00 | 0 | 00:00:00 |
| 1999-12-31 09:00:00 | 32400 | 09:00:00 |
| 2000-06-04 15:45:30 | 56730 | 15:45:30 |
| 2017-03-16 12:30:15 | 45015 | 12:30:15 |
+---------------------+----------------------+-------------------+
8 rows in set (0.00 sec)mysql> select ts, time_to_sec(ts) as 'time part in seconds',-> sec_to_time(time_to_sec(ts)) as 'time part as TIME'-> from timestamp_val;
+---------------------+----------------------+-------------------+
| ts | time part in seconds | time part as TIME |
+---------------------+----------------------+-------------------+
| 1970-01-01 05:00:00 | 18000 | 05:00:00 |
| 1987-03-05 08:30:15 | 30615 | 08:30:15 |
| 1999-12-31 05:00:00 | 18000 | 05:00:00 |
| 2000-06-04 11:45:30 | 42330 | 11:45:30 |
+---------------------+----------------------+-------------------+
4 rows in set (0.00 sec)--在日期值和天数之间进行转换
mysql> select d, to_days(d) as 'date to days', from_days(to_days(d)) as 'DATE to days to DATE' from date_val;
+------------+--------------+----------------------+
| d | date to days | DATE to days to DATE |
+------------+--------------+----------------------+
| 1864-02-28 | 680870 | 1864-02-28 |
| 1900-01-15 | 693975 | 1900-01-15 |
| 1999-12-31 | 730484 | 1999-12-31 |
| 2000-06-04 | 730640 | 2000-06-04 |
| 2017-03-16 | 736769 | 2017-03-16 |
| 1864-02-28 | 680870 | 1864-02-28 |
| 1900-01-15 | 693975 | 1900-01-15 |
| 1999-12-31 | 730484 | 1999-12-31 |
| 2000-06-04 | 730640 | 2000-06-04 |
| 2017-03-16 | 736769 | 2017-03-16 |
| 2007-05-13 | 733174 | 2007-05-13 |
+------------+--------------+----------------------+
11 rows in set (0.00 sec)mysql> select dt, to_days(dt) as 'date part in days', from_days(to_days(dt)) as 'date part as DATE' from datetime_val;
+---------------------+-------------------+-------------------+
| dt | date part in days | date part as DATE |
+---------------------+-------------------+-------------------+
| 1970-01-01 00:00:00 | 719528 | 1970-01-01 |
| 1999-12-31 09:00:00 | 730484 | 1999-12-31 |
| 2000-06-04 15:45:30 | 730640 | 2000-06-04 |
| 2017-03-16 12:30:15 | 736769 | 2017-03-16 |
| 1970-01-01 00:00:00 | 719528 | 1970-01-01 |
| 1999-12-31 09:00:00 | 730484 | 1999-12-31 |
| 2000-06-04 15:45:30 | 730640 | 2000-06-04 |
| 2017-03-16 12:30:15 | 736769 | 2017-03-16 |
+---------------------+-------------------+-------------------+
8 rows in set (0.00 sec)mysql> select ts, to_days(ts) as 'date part in days', from_days(to_days(ts)) as 'date part as DATE' from timestamp_val;
+---------------------+-------------------+-------------------+
| ts | date part in days | date part as DATE |
+---------------------+-------------------+-------------------+
| 1970-01-01 05:00:00 | 719528 | 1970-01-01 |
| 1987-03-05 08:30:15 | 725800 | 1987-03-05 |
| 1999-12-31 05:00:00 | 730484 | 1999-12-31 |
| 2000-06-04 11:45:30 | 730640 | 2000-06-04 |
+---------------------+-------------------+-------------------+
4 rows in set (0.00 sec)
--在datetime或者timestamp类型值和秒数之间进行转换
mysql> select dt, unix_timestamp(dt) as seconds, from_unixtime(unix_timestamp(dt)) as timestamp from datetime_val;
+---------------------+------------+---------------------+
| dt | seconds | timestamp |
+---------------------+------------+---------------------+
| 1970-01-01 00:00:00 | 0 | 1970-01-01 04:00:00 |
| 1999-12-31 09:00:00 | 946616400 | 1999-12-31 09:00:00 |
| 2000-06-04 15:45:30 | 960119130 | 2000-06-04 15:45:30 |
| 2017-03-16 12:30:15 | 1489653015 | 2017-03-16 12:30:15 |
| 1970-01-01 00:00:00 | 0 | 1970-01-01 04:00:00 |
| 1999-12-31 09:00:00 | 946616400 | 1999-12-31 09:00:00 |
| 2000-06-04 15:45:30 | 960119130 | 2000-06-04 15:45:30 |
| 2017-03-16 12:30:15 | 1489653015 | 2017-03-16 12:30:15 |
+---------------------+------------+---------------------+
8 rows in set (0.00 sec)mysql> select curdate(), unix_timestamp(curdate()), from_unixtime(unix_timestamp(curdate()))\G
*************************** 1. row ***************************curdate(): 2025-06-10unix_timestamp(curdate()): 1749499200
from_unixtime(unix_timestamp(curdate())): 2025-06-10 00:00:00
1 row in set (0.00 sec)
6.9 计算两个日期或时间之间的间隔
--使用时间差函数
set @d1 = '2010-01-01', @d2 = '2009-12-01';
select datediff(@d1, @d2) as 'd1 - d2', datediff(@d2, @d1) as 'd2 - d1';set @t1 = '12:00:00', @t2 = '16:30:00';
select timediff(@t1, @t2) as 't1 - t2', timediff(@t2, @t1) as 't2 - t1';select t1, t2, timediff(t2, t1) as 't2 - t1 as TIME', if(timediff(t2, t1) >= 0, '+', '-') as sign,hour(timediff(t2, t1)) as hour,minute(timediff(t2, t1)) as minute,second(timediff(t2, t1)) as second
from time_val;set @dt1 = '1900-01-01 00:00:00', @dt2 = '1910-01-01 00:00:00';
select timestampdiff(minute, @dt1, @dt2) as minutes,timestampdiff(hour, @dt1, @dt2) as hours,timestampdiff(day, @dt1, @dt2) as days,timestampdiff(week, @dt1, @dt2) as weeks,timestampdiff(year, @dt1, @dt2) as years;--利用基本时间单位计算时间间隔
select t1, t2, time_to_sec(t2) - time_to_sec(t1) as 't2 - t1 (in seconds)',sec_to_time(time_to_sec(t2) - time_to_sec(t1)) as 't2 - t1 (as TIME)'
from time_val;--使用基本单位计算两个Date类型值,或者两个date-and-time类型值的时间间隔
select to_days('1884-01-01') - to_days('1883-06-05') as days;
select (to_days('1884-01-01') - to_days('1883-06-05')) / 7 as weeks;set @dt1 = '1984-01-01 09:00:00';
set @dt2 = @dt1 + interval 14 day;
select unix_timestamp(@dt2) - unix_timestamp(@dt1) as seconds;set @interval = unix_timestamp(@dt2) - unix_timestamp(@dt1);
select @interval as seconds,@interval / 60 as minutes,@interval / (60 * 60) as hours,@interval / (24 * 60 * 60) as days,@interval / (7 * 24 * 60 * 60) as weeks;set @dt1 = '1800-02-14 07:30:00';
set @dt2 = @dt1 + interval 7 day;
set @interval = ((to_days(@dt2) - to_days(@dt1)) * 24 * 60 * 60)+ time_to_sec(@dt2) - time_to_sec(@dt1);select @interval as seconds, sec_to_time(@interval) as time;
6.10 增加日期或时间值
--使用时间加法函数或者操作符进行时间值求和运算
mysql> set @t1 = '12:00:00', @t2 = '15:30:00';
Query OK, 0 rows affected (0.00 sec)mysql> select addtime(@t1, @t2);
+-------------------+
| addtime(@t1, @t2) |
+-------------------+
| 27:30:00 |
+-------------------+
1 row in set (0.00 sec)mysql>
mysql> set @dt = '1984-03-01 12:00:00', @t = '12:00:00';
Query OK, 0 rows affected (0.00 sec)mysql> select addtime(@dt, @t);
+---------------------+
| addtime(@dt, @t) |
+---------------------+
| 1984-03-02 00:00:00 |
+---------------------+
1 row in set (0.00 sec)mysql>
mysql> set @d = '1984-03-01', @t = '15:30:00';
Query OK, 0 rows affected (0.00 sec)mysql> select timestamp(@d, @t);
+----------------------------+
| timestamp(@d, @t) |
+----------------------------+
| 1984-03-01 15:30:00.000000 |
+----------------------------+
1 row in set (0.00 sec)mysql>
mysql> set @dt = '1984-03-01 12:00:00', @t = '12:00:00';
Query OK, 0 rows affected (0.00 sec)mysql> select timestamp(@dt, @t);
+----------------------------+
| timestamp(@dt, @t) |
+----------------------------+
| 1984-03-02 00:00:00.000000 |
+----------------------------+
1 row in set (0.00 sec)--求出即日第三天的日期值
mysql> select curdate(), date_add(curdate(), interval 3 day);
+------------+-------------------------------------+
| curdate() | date_add(curdate(), interval 3 day) |
+------------+-------------------------------------+
| 2025-06-10 | 2025-06-13 |
+------------+-------------------------------------+
1 row in set (0.00 sec)
--一个星期前的日期值
mysql> select curdate(), date_sub(curdate(), interval 7 day);
+------------+-------------------------------------+
| curdate() | date_sub(curdate(), interval 7 day) |
+------------+-------------------------------------+
| 2025-06-10 | 2025-06-03 |
+------------+-------------------------------------+
1 row in set (0.00 sec)
--60小时之后是什么时间(同时需要日期和时间值)
mysql> select now(), date_add(now(), interval 60 hour);
+---------------------+-----------------------------------+
| now() | date_add(now(), interval 60 hour) |
+---------------------+-----------------------------------+
| 2025-06-10 16:50:34 | 2025-06-13 04:50:34 |
+---------------------+-----------------------------------+
1 row in set (0.00 sec)mysql>
mysql> select now(), date_add(now(), interval '14:30' hour_minute);
+---------------------+-----------------------------------------------+
| now() | date_add(now(), interval '14:30' hour_minute) |
+---------------------+-----------------------------------------------+
| 2025-06-10 16:50:34 | 2025-06-11 07:20:34 |
+---------------------+-----------------------------------------------+
1 row in set (0.00 sec)mysql> select now(), date_add(now(), interval '3 4' day_hour);
+---------------------+------------------------------------------+
| now() | date_add(now(), interval '3 4' day_hour) |
+---------------------+------------------------------------------+
| 2025-06-10 16:50:34 | 2025-06-13 20:50:34 |
+---------------------+------------------------------------------+
1 row in set (0.00 sec)mysql> select curdate(), curdate() + interval 1 year;
+------------+-----------------------------+
| curdate() | curdate() + interval 1 year |
+------------+-----------------------------+
| 2025-06-10 | 2026-06-10 |
+------------+-----------------------------+
1 row in set (0.00 sec)mysql> select now(), now() - interval '1 12' day_hour;
+---------------------+----------------------------------+
| now() | now() - interval '1 12' day_hour |
+---------------------+----------------------------------+
| 2025-06-10 16:50:36 | 2025-06-09 04:50:36 |
+---------------------+----------------------------------+
1 row in set (0.00 sec)--使用基本时间单位进行时间加法运算
mysql> select t1, sec_to_time(time_to_sec(t1) + 7200) as 't1 plus 2 hours' from time_val;
+----------+-----------------+
| t1 | t1 plus 2 hours |
+----------+-----------------+
| 15:00:00 | 17:00:00 |
| 05:01:30 | 07:01:30 |
| 12:30:20 | 14:30:20 |
+----------+-----------------+
3 rows in set (0.00 sec)mysql>
mysql> select t1, t2, time_to_sec(t1) + time_to_sec(t2) as 't1 + t2 (in seconds)',-> sec_to_time(time_to_sec(t1) + time_to_sec(t2)) as 't1 + t2 (as TIME)'-> from time_val;
+----------+----------+----------------------+-------------------+
| t1 | t2 | t1 + t2 (in seconds) | t1 + t2 (as TIME) |
+----------+----------+----------------------+-------------------+
| 15:00:00 | 15:00:00 | 108000 | 30:00:00 |
| 05:01:30 | 02:30:20 | 27110 | 07:31:50 |
| 12:30:20 | 17:30:45 | 108065 | 30:01:05 |
+----------+----------+----------------------+-------------------+
3 rows in set (0.00 sec)mysql>
mysql>
mysql> select t1, t2, mod(time_to_sec(t1) + time_to_sec(t2), 86400) as 't1 + t2 (in seconds)',-> sec_to_time(mod(time_to_sec(t1) + time_to_sec(t2), 86400)) as 't1 + t2 (as TIME)'-> from time_val;
+----------+----------+----------------------+-------------------+
| t1 | t2 | t1 + t2 (in seconds) | t1 + t2 (as TIME) |
+----------+----------+----------------------+-------------------+
| 15:00:00 | 15:00:00 | 21600 | 06:00:00 |
| 05:01:30 | 02:30:20 | 27110 | 07:31:50 |
| 12:30:20 | 17:30:45 | 21665 | 06:01:05 |
+----------+----------+----------------------+-------------------+
3 rows in set (0.00 sec)mysql>
mysql> set @d = '2006-01-01';
Query OK, 0 rows affected (0.00 sec)mysql> select @d as date, from_days(to_days(@d) + 7) as 'date + 1 week',-> from_days(to_days(@d) - 7) as 'date - 1 week';
+------------+---------------+---------------+
| date | date + 1 week | date - 1 week |
+------------+---------------+---------------+
| 2006-01-01 | 2006-01-08 | 2005-12-25 |
+------------+---------------+---------------+
1 row in set (0.00 sec)mysql>
mysql> set @dt = '2006-01-01 12:30:45';
Query OK, 0 rows affected (0.00 sec)mysql> select @dt as datetime,-> from_days(to_days(@dt) + 7) as 'datetime + 1 week',-> from_days(to_days(@dt) - 7) as 'datetime - 1 week';
+---------------------+-------------------+-------------------+
| datetime | datetime + 1 week | datetime - 1 week |
+---------------------+-------------------+-------------------+
| 2006-01-01 12:30:45 | 2006-01-08 | 2005-12-25 |
+---------------------+-------------------+-------------------+
1 row in set (0.00 sec)mysql>
mysql> set @dt = '2006-01-01 09:00:00';
Query OK, 0 rows affected (0.00 sec)mysql> select @dt as datetime,-> from_unixtime(unix_timestamp(@dt) + 3600) as 'datetime + 1 hour',-> from_unixtime(unix_timestamp(@dt) - 3600) as 'datetime - 1 hour';
+---------------------+----------------------------+----------------------------+
| datetime | datetime + 1 hour | datetime - 1 hour |
+---------------------+----------------------------+----------------------------+
| 2006-01-01 09:00:00 | 2006-01-01 10:00:00.000000 | 2006-01-01 08:00:00.000000 |
+---------------------+----------------------------+----------------------------+
1 row in set (0.00 sec)