一、日期转字符串

1、函数:date_format(date, format)

2、例:

    select date_format(now(),'%Y-%m-%d %H:%i:%S');
    结果:2017-10-29 14:02:54
select date_format(now(),'%Y-%m-%d %H:%i:%S');
    结果:2017-10-29 14:02:54
 

二、日期转时间戳

1、函数:unix_timestamp(data)

2、例:

    select unix_timestamp(now());  
    结果:1509257408
select unix_timestamp(now());  
    结果:1509257408
 

三、字符串转日期

1、函数:str_to_date(str,format);注:format格式必须和str的格式相同,否则返回空

2、例:

    select str_to_date('2017-10-29', '%Y-%m-%d %H:%i:%S');
      结果:2017-10-29 00:00:00 
select str_to_date('2017-10-29', '%Y-%m-%d %H:%i:%S');
      结果:2017-10-29 00:00:00 
 

四、时间戳转日期

1、函数:from_unixtime(time-stamp);

2、例:

    select from_unixtime(1509257408); 
      结果:2017-10-29 14:10:08
select from_unixtime(1509257408); 
      结果:2017-10-29 14:10:08
 

五、时间戳转字符串

1、函数:from_unixtime(time-stamp,format);

 2、例:

    select from_unixtime(1509257408,'%Y~%m~%d %H:%i:%S'); 
    结果:2017~10~29 14:02:08
select from_unixtime(1509257408,'%Y~%m~%d %H:%i:%S'); 
    结果:2017~10~29 14:02:08
 

附录:

MySQL日期格式化(format)取值范围。

 

 参考:http://www.cnblogs.com/wangyongwen/p/6265126.html

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐