mysql数据库-日期时间函数

后端 / 2021-08-23

current_date

当前日期

# 当前日期
select current_date() from  dual;

current_time

当前时间

# 当前时间
select current_time
from dual;

# 当前日期时间
select concat(current_date,' ',current_time())
from dual;

current_timestamp

当前时间戳

# 当前时间戳
select current_timestamp()
from dual;

date

当前日期部分

# 获取日期
select date('1999-05-07 00:00:00,')
from dual;

time

当前时间部分

# 获取时间
select time ('1999-05-07 00:00:00,')
from dual;

date_add

日期相加

# 日期相加
select date_add(current_date(),INTERVAL 2 day )
from dual;

date_sub

日期相减

# 日期相减

select date_sub(current_date(),INTERVAL 3 day )
from dual;

date_diff

两个日期差

#  日期差
select DATEDIFF(date_add(current_date(),INTERVAL 2 day ),date_sub(current_date(),INTERVAL 3 day )) as diff
from dual;

time_diff

两个时间差

# 时间差
select timediff(current_time(),'12:00:00')
from dual;

now

当前时间

# 当前时间
select now()
from dual;

year|month|day

年 月 日

# 年月日
select YEAR('1999-05-07' ) as year,MONTH('1999-05-07' ) as mon,DAY('1999-05-07' ) as day
from dual;

for_unixtime

转换为 unix时间 (1970-1-1 到现在 经历了多少秒)

# unix 时间
select from_unixtime('2007 30th November 10:30:59 2007')
from dual;



# unix时间格式化
select from_unixtime(1629121805,'%Y-%m-%d %H:%i:%s')
from dual;

# 1970 1 -1 到现在经历的秒数
select unix_timestamp()
from dual;