mysql日期转换函数有哪些

来自:互联网
时间:2021-12-02
阅读:

MySQL 日期转换函数、时间转换函数

1,MySQL Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format), time_format(time,format)

函数:date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s')

结果:20080808222301

MySQL 日期、时间转换函数:date_format(date,format)time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式。它是 str_to_date(str,format) 函数的 一个逆转换。

2,MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)

select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09
select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09
select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30

str_to_date(str,format) 转换函数,可以把一些杂乱无章的字符串转换为日期格式。

3,MySQL (日期、天数)转换函数:to_days(date)from_days(days)

select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627

4,MySQL (时间、秒)转换函数:time_to_sec(time)sec_to_time(seconds)

select time_to_sec('01:00:05'); -- 3605
select sec_to_time(3605); -- '01:00:05'

5,MySQL 拼凑日期、时间函数:makdedate(year,dayofyear)maketime(hour,minute,second)

select makedate(2001,31); -- '2001-01-31'
select makedate(2001,32); -- '2001-02-01'
select maketime(12,15,30); -- '12:15:30'

6,MySQL (Unix 时间戳、日期)转换函数:

unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)
返回顶部
顶部