mysql: 日期、时间函数总结

1、获得当前日期时间 函数 

 curdate()/curtime()/now()/sysdate()

sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。

2、时间戳(Timestamp)函数        # 时间戳:是指从1970年1月1日0点00分00秒到现在的秒数。作用是数值方便比较和运算。

current_timestamp/current_timestamp()

(Unix 时间戳、日期)转换函数:

将日期转化成时间戳:unix_timestamp(date)

将时间戳转化为日期:from_unixtime(unix_timestamp)/from_unixtime(unix_timestamp,format)

时间戳(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()。

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

MySQL timestampdiff() 函数就比 datediff() 功能强多了,datediff() 只能计算两个日期(date)之间相差的天数。

 

转化函数:date_format(date,format), time_format(time,format)

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

转换函数:to_days(date), from_days(days)

select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627

 

MySQL (时间、秒)转换函数: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'

日期时间计算函数

 

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

date_add(from_unixtime(unix_timestamp(event_day,'yyyymmdd'),'yyyy-mm-dd'), -30)往前推30天。

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 |
+----------------------------------------------------------------+

MySQL 日期时间 Extract(选取) 函数。 
1. 选取日期时间的各个部分:日期、时间、年、季度、月、日、小时、分钟、秒、微秒

set @dt = '2008-09-10 07:15:30.123456';

select date(@dt);        -- 2008-09-10 
select time(@dt);        -- 07:15:30.123456 
select year(@dt);        -- 2008 
select quarter(@dt);     -- 3 
select month(@dt);       -- 9 
select week(@dt);        -- 36 
select day(@dt);         -- 10 
select hour(@dt);        -- 7 
select minute(@dt);      -- 15 
select second(@dt);      -- 30 
select microsecond(@dt); -- 123456

2. MySQL Extract() 函数,可以上面实现类似的功能:

set @dt = '2008-09-10 07:15:30.123456';

select extract(year                from @dt); -- 2008 
select extract(quarter             from @dt); -- 3 
select extract(month               from @dt); -- 9 
select extract(week                from @dt); -- 36 
select extract(day                 from @dt); -- 10 
select extract(hour                from @dt); -- 7 
select extract(minute              from @dt); -- 15 
select extract(second              from @dt); -- 30 
select extract(microsecond         from @dt); -- 123456

select extract(year_month          from @dt); -- 200809 
select extract(day_hour            from @dt); -- 1007 
select extract(day_minute          from @dt); -- 100715 
select extract(day_second          from @dt); -- 10071530 
select extract(day_microsecond     from @dt); -- 10071530123456 
select extract(hour_minute         from @dt); --    715 
select extract(hour_second         from @dt); --    71530 
select extract(hour_microsecond    from @dt); --    71530123456 
select extract(minute_second       from @dt); --      1530 
select extract(minute_microsecond from @dt); --      1530123456 
select extract(second_microsecond from @dt); --        30123456

 函数:dayofweek(), dayofmonth(), dayofyear()

分别返回日期参数,在一周、一月、一年中的位置。

set @dt = '2008-08-08';

select dayofweek(@dt);   -- 6 
select dayofmonth(@dt); -- 8 
select dayofyear(@dt);   -- 221

日期 '2008-08-08' 是一周中的第 6 天(1 = Sunday, 2 = Monday, ..., 7 = Saturday);一月中的第 8 天;一年中的第 221 天。

last_day() 函数:返回月份中的最后一天。

select last_day('2008-02-01'); -- 2008-02-29 
select last_day('2008-08-08'); -- 2008-08-31

MySQL last_day() 函数非常有用,比如我想得到当前月份中有多少天,可以这样来计算:

mysql> select now(), day(last_day(now())) as days;

+---------------------+------+ 
| now()               | days | 
+---------------------+------+ 
| 2008-08-09 11:45:45 |   31 | 
+---------------------+------+

三、MySQL 日期时间计算函数 
1. 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_add() 函数,分别为 @dt 增加了“1小时 15分 30秒” 和 “1天 1小时 15分 30秒”。建议:总是使用 date_add() 日期时间函数来替代 adddate(), addtime()。

2. MySQL 为日期减去一个时间间隔: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                                            | 
+----------------------------------------------------------------+

MySQL date_sub() 日期时间函数 和 date_add() 用法一致,不再赘述。另外,MySQL 中还有两个函数 subdate(), subtime(),建议,用 date_sub() 来替代。

4. MySQL 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)

MySQL datediff(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) 函数的两个参数类型必须相同。

1. MySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)

select time_to_sec('01:00:05'); -- 3605 
select sec_to_time(3605);        -- '01:00:05'

2. MySQL (日期、天数)转换函数:to_days(date), from_days(days)

select to_days('0000-00-00'); -- 0 
select to_days('2008-08-08'); -- 733627

select from_days(0);           -- '0000-00-00' 
select from_days(733627);      -- '2008-08-08'

4. MySQL Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format), time_format(time,format)

mysql> select date_format('2008-08-08 22:23:00', '%W %M %Y');

+------------------------------------------------+ 
| date_format('2008-08-08 22:23:00', '%W %M %Y') | 
+------------------------------------------------+ 
| Friday August 2008                             | 
+------------------------------------------------+

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                                     | 
+----------------------------------------------------+

mysql> select time_format('22:23:01', '%H.%i.%s');

+-------------------------------------+ 
| time_format('22:23:01', '%H.%i.%s') | 
+-------------------------------------+ 
| 22.23.01                            | 
+-------------------------------------+

MySQL 日期、时间转换函数:date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式。它是 str_to_date(str,format) 函数的 一个逆转换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值