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
注意:本篇文章仅用于个人学习,如有侵权,联系删除!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)