获取当前日期时间函数
获取包含日期和时间的当前值,可使用 now() 函数。
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2008-08-08 22:20:46 |
+---------------------+
同样可以获取包含日期和时间的当前值,使用 sysdate() 函数。
sysdate() 与 now() 均用于获取日期时间,但行为不同:now() 在语句执行开始时即确定返回值,而 sysdate() 则在函数实际执行时才动态获取当前值。通过对比示例可直观理解这一区别。
mysql> select now(), sleep(3), now();
+---------------------+----------+---------------------+
| now() | sleep(3) | now() |
+---------------------+----------+---------------------+
| 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 |
+---------------------+----------+---------------------+
在实际开发中,sysdate() 日期时间函数较少被使用。
若需获取当前时间戳,MySQL 提供 current_timestamp 或 current_timestamp() 函数。
mysql> select current_timestamp, current_timestamp();
+---------------------+---------------------+
| current_timestamp | current_timestamp() |
+---------------------+---------------------+
| 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |
+---------------------+---------------------+
日期与时间格式转换
MySQL 提供了丰富的日期与时间转换工具。
将日期或时间转换为字符串,可使用 date_format(date, format) 和 time_format(time, format) 函数。
mysql> select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s');
+----------------------------------------------------+
| date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s') |
+----------------------------------------------------+
| 20080808222301 |
+----------------------------------------------------+
date_format(date, format) 与 time_format(time, format) 能够将日期或时间格式化为指定的字符串形式。该功能与 str_to_date(str, format) 互为逆操作。
将字符串解析为日期,可使用 str_to_date(str, format) 函数。
select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09
select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09
select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30
str_to_date(str, format) 能够把杂乱无章的字符串解析为标准日期格式,同时也支持解析为时间格式。具体的格式代码请参考 MySQL 官方手册。
日期天数与时间秒数转换
涉及日期与天数的转换,可使用 to_days(date) 和 from_days(days) 函数。
select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627
涉及时间与秒数的转换,可使用 time_to_sec(time) 和 sec_to_time(seconds) 函数。
select time_to_sec('01:00:05'); -- 3605
select sec_to_time(3605); -- '01:00:05'
用于拼凑日期和时间的函数包括 makedate(year, dayofyear) 和 maketime(hour, minute, second)。
select makedate(2001,31); -- '2001-01-31'
select makedate(2001,32); -- '2001-02-01'
select maketime(12,15,30); -- '12:15:30'
Unix 时间戳转换
MySQL 还提供 Unix 时间戳与标准日期之间的相互转换功能。
unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)
以下是相关函数的具体使用示例。
select unix_timestamp(); -- 1218290027
select unix_timestamp('2008-08-08'); -- 1218124800
select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800
select from_unixtime(1218290027); -- '2008-08-09 21:53:47'
select from_unixtime(1218124800); -- '2008-08-08 00:00:00'
select from_unixtime(1218169800); -- '2008-08-08 12:30:00'
select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '2008 8th August 12:30:00 2008'
日期时间算术计算
MySQL 支持对日期和时间进行各种算术计算。
若需为日期增加特定的时间间隔,可使用 date_add() 函数。
set @dt = now();
select date_add(@dt, interval 1 day); -- add 1 day
select date_add(@dt, interval 1 hour); -- add 1 hour
select date_add(@dt, interval 1 minute); -- ...
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);
select date_add(@dt, interval -1 day); -- sub 1 day
虽然 MySQL 也提供 adddate() 和 addtime() 函数,但它们的功能均可由 date_add() 替代。以下示例展示了如何使用 date_add() 实现类似 addtime() 的效果。
mysql> set @dt = '2008-08-09 12:12:33';
mysql>
mysql> select date_add(@dt, interval '01:15:30' hour_second);
+------------------------------------------------+
| date_add(@dt, interval '01:15:30' hour_second) |
+------------------------------------------------+
| 2008-08-09 13:28:03 |
+------------------------------------------------+
mysql> select date_add(@dt, interval '1 01:15:30' day_second);
+-------------------------------------------------+
| date_add(@dt, interval '1 01:15:30' day_second) |
+-------------------------------------------------+
| 2008-08-10 13:28:03 |
+-------------------------------------------------+
若需从日期中减去特定的时间间隔,可使用 date_sub() 函数。
mysql> select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);
+----------------------------------------------------------------+
| date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second) |
+----------------------------------------------------------------+
| 1997-12-30 22:58:59 |
+----------------------------------------------------------------+
date_sub() 日期时间函数的用法与 date_add() 完全一致,此处不再重复说明。
日期时间相减与时间戳操作
MySQL 提供 datediff(date1,date2), timediff(time1,time2) 函数用于日期和时间的相减运算。
datediff(date1, date2) 函数用于计算两个日期 date1 与 date2 之间相差的天数(即 date1 - date2)。
select datediff('2008-08-08', '2008-08-01'); -- 7
select datediff('2008-08-01', '2008-08-08'); -- -7
MySQL timediff(time1,time2):两个日期相减 time1 - time2,返回 time 差值。
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08
select timediff('08:08:08', '00:00:00'); -- 08:08:08
需要注意的是,timediff(time1, time2) 函数的两个参数类型必须保持一致。
MySQL 还支持时间戳(timestamp)的转换及增减操作。
timestamp(date) -- date to timestamp
timestamp(dt,time) -- dt + time
timestampadd(unit,interval,datetime_expr) --
timestampdiff(unit,datetime_expr1,datetime_expr2) --
具体示例如下所示。
select timestamp('2008-08-08'); -- 2008-08-08 00:00:00
select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01
select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01
select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00
select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00
MySQL timestampadd() 函数类似于 date_add()。
select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1
select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485
select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00'); -- -12
select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00'); -- 7
相比只能计算天数差的 datediff(),timestampdiff() 功能更为强大,支持更精细的时间单位计算。
时区转换方法
MySQL 具备处理时区(timezone)转换的能力。
convert_tz(dt,from_tz,to_tz)
select convert_tz('2008-08-08 12:00:00', '+08:00', '+00:00'); -- 2008-08-08 04:00:00
除了专用函数,时区转换也可以通过 date_add、date_sub 或 timestampadd 来实现。
select date_add('2008-08-08 12:00:00', interval -8 hour); -- 2008-08-08 04:00:00
select date_sub('2008-08-08 12:00:00', interval 8 hour); -- 2008-08-08 04:00:00
select timestampadd(hour, -8, '2008-08-08 12:00:00'); -- 2008-08-08 04:00:00
以上介绍了 MySQL 中获取和处理当前日期时间的各类函数,希望能对大家有所帮助。如有任何疑问,欢迎留言,小编会及时回复!