【mysql时间类sql处理总结】

获取当前时间:【年-月-日 时:分:秒】

-- 格式:2020-05-15 19:57:47
select now() from test -- 建议
select current_timestamp from test
select current_timestamp() from test
select localtime from test
select localtime() from test

获取当前时间:【年-月-日】

-- 格式:2020-05-15
select curdate() from test -- 建议
select current_date from test
select current_date() from test

获取当前时间:【时:分:秒】

-- 格式:19:06:49
select curtime() from test
select current_time from test
select current_time() from test

获取月份中的最后一天:【年-月-日】

-- 格式:2020-02-29
select last_day('2020-02-01');

获取当前月份有多少天:【天数】

-- 格式:31
select day(last_day(now())) as days;

获取两个时间之间得天数:【天数】

-- 格式 4
select datediff('2020-05-05', '2020-05-01') from test;

获取时间得差值:【时:分:秒】

-- 格式:08:08:08
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00') from test;

时间-字符串-时间戳之间的转换

时间转字符串:

-- 格式:2020-05-15 19:23:25
select date_format(now(), '%Y-%m-%d %H:%i:%s');

时间转时间戳:

-- 格式:1589531039
select unix_timestamp(now());

时间戳转字符串:

-- 格式:2018-05-02 20:16:23
select from_unixtime(1525263383, '%Y-%m-%d %H:%i:%s');

时间戳转时间:

-- 2018-05-02 20:16:23
select from_unixtime(1525263383);

字符串转时间:

-- 格式:2020-05-02 19:20:20
select str_to_date('2020-05-02 19:20:20','%Y-%m-%d %H:%i:%s');

字符串转时间戳:

-- 1525263850
select unix_timestamp('2018-05-02 20:24:10');

 

 

 

 

 

 

 

 

 

 

 

参考:

1. https://www.cnblogs.com/blueness-sunshine/p/4912058.html

2. https://www.cnblogs.com/zuiyue_jing/p/12769486.html

注意:本篇文章仅用于个人学习,如有侵权,联系删除!!!

持续更新!!!

posted @ 2020-05-15 16:31  夏夜凉凉  阅读(203)  评论(0编辑  收藏  举报