mysql日期和字符串相互转化

目的:整理mysql中日期和字符串的相互转化的一些函数

一、日期转字符串

1、函数:date_format(date, format)

2、例:

	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


三、字符串转日期

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 


四、时间戳转日期

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

2、例:

	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

附录:

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


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


猜你喜欢

转载自blog.csdn.net/qq_33157666/article/details/78385115