mysql获取时间、时间戳、时间戳与时间互转函数
如下图所示
part1
# now yyyy-mm-dd hh:ii:ss
# current_timestamp yyyy-mm-dd hh:ii:ss
# curdate() yyyy-mm-dd
# curtime() hh:ii:ss
# date('yyyy-mm-dd hh:ii:ss') yyyy-mm-dd
# year('yyyy-mm-dd hh:ii:ss') yyyy
# month('yyyy-mm-dd hh:ii:ss') mm
# day('yyyy-mm-dd hh:ii:ss') dd
# time('yyyy-mm-dd hh:ii:ss') hh:ii:ss
# hour('yyyy-mm-dd hh:ii:ss') hh
# minute('yyyy-mm-dd hh:ii:ss') ii
# second('yyyy-mm-dd hh:ii:ss') ss
SELECT
now( ) as "0",
current_timestamp as "1",
curdate( )as "2",
curtime( )as "3",
date('2022-10-11 12:13:14')as "4",
year('2022-10-11 12:13:14')as "5",
month('2022-10-11 12:13:14')as "6",
day('2022-10-11 12:13:14')as "7",
time('2022-10-11 12:13:14')as "8",
hour('2022-10-11 12:13:14')as "9",
minute('2022-10-11 12:13:14')as "10",
second('2022-10-11 12:13:14')as "11"
prat2
# current_timestamp 当前时间 yyyy-mm-dd hh:ii:ss
# unix_timestamp 当前时间的时间戳 单位秒
# unix_timestamp(date) 将date转换为 时间戳
# date_format(date, format)
# time_format(date, format) 提取date时间当中的时分秒format 不能提取年月日
# str_to_date(str,format)
# from_unixtime(timestamp) 将timestamp时间戳转换为 yyyy-mm-dd hh:ii:ss
# from_unixtime(timestamp,format) 将timestamp时间戳转换为 format格式
# 将当前时间转为时间戳
select
current_timestamp as "1",
unix_timestamp() as "2",
unix_timestamp('2022-12-12 16:07:37') as "3",
date_format('2022-12-12 12:12:12', '%Y-%m-%d %T') as "4",
time_format('2022-12-12 11:12:13', '%H:%i:%s') as "41",
str_to_date('2022-12-12 12:12:12','%Y-%m-%d') as "5",
from_unixtime(1670832457) as "6",
from_unixtime(1670832457, '%Y-%m-%d %H:%i:%s') as "7"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)