mysql 日期转换

6、日期转换

6.1、日期、字符串互转

  • date_format(date, format)日期转字符串
-- 同上,日期转换格式,即可转换字符串
date_format(date, format) 函数用于以不同的格式显示日期/时间数据。
TIME_FORMAT(time,format) 时间格式化
  • tr_to_date(str,format) 字符串转日期
-- 将字符串转换为日期
select str_to_date('08/09/2008', '%m/%d/%Y');

6.1.2、(日期、天数)转换函数

  • to_days(date) :将日期转换成总天数
  • from_days(days) 将总天数转换成日期
select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627

6.1.3. (时间、秒)转换函数:

  • time_to_sec(time) 将时间转换总秒数
  • sec_to_time(seconds) :将总秒数转换成时间
select time_to_sec('01:00:05'); -- 3605
select sec_to_time(3605); -- '01:00:05'

6.2 日期(字符串)转时间戳

2.1、日期时间戳互转

  • unix_timestamp(date) 日期/字符串转时间戳
  • rom_unixtime(unix_timestamp, format) 时间戳转日期
-- 日期时间转时间戳
select unix_timestamp(now());
#结果:1452001082
-- 时间戳转日期
select from_unixtime(1451997924);
#结果:2016-01-05 20:45:24

2.2、字符串时间戳互转

-- 字符串转时间搓
select unix_timestamp('2016-01-02');
#结果:1451664000
-- 时间戳转字符串
select from_unixtime(1451997924,'%Y-%d');
#结果:2016-01-05 20:45:24

版权声明:本文为weixin_43660536原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。