MySQL 时间戳日期互转

WechatIMG263.jpeg

  • 时间转字符串
1
2
select date_format(now(), '%Y-%m-%d');
# 结果:2016-01-05
  • 时间转时间戳
1
2
select unix_timestamp(now());  
# 结果:1452001082
  • 字符串转时间
1
2
select str_to_date('2016-01-02', '%Y-%m-%d %H');  
# 结果:2016-01-02 00:00:00
  • 字符串转时间戳
1
2
select unix_timestamp('2016-01-02');  
# 结果:1451664000
  • 时间戳转日期,默认
1
2
select from_unixtime(UNIX_TIMESTAMP());
# 结果:2022-10-27 11:08:15
  • 时间戳转日期,指定格式
1
2
select from_unixtime(UNIX_TIMESTAMP(),'%Y-%m-%d');  
# 结果:2022-10-27
-------------本文结束感谢您的阅读-------------
0%