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
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值