项目中常有date转string、date转时间戳以及三种格式的互相转换,这里整理一下。
格式转换相关函数:
date_format(date, format) ,
unix_timestamp()
str_to_date(str, format)
from_unixtime(unix_timestamp, format)
example :
1、时间转字符串:
select date_format(now(), '%Y-%m-%d');
#结果:2019-04-04
2、时间转时间戳:
select unix_timestamp(now());
#结果:1554363917
3、字符串转时间:
select str_to_date('2016-01-02', '%Y-%m-%d %H');
#结果:2019-04-03 00:00:00
4、字符串转时间戳:
select unix_timestamp('2019-04-04');
#结果:1554307200
5、时间戳转时间:
select from_unixtime(1554307200);
6、时间戳转字符串:
select from_unixtime(1451997924,'%Y-%m-%d');
-- 结果:2019-04-04