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"