天下事有难易乎?为之,则难者亦易矣;不为,则易者亦难矣。

Mysql中时间 Date和字符串,时间戳间相互转换

往事如烟 5224次浏览 0个评论

时间、字符串、时间戳之间的互相转换很常用,但是几乎每次使用时候都喜欢去搜索一下用法;本文整理一下三者之间的 转换(即:date转字符串、date转时间戳、字符串转date、字符串转时间戳、时间戳转date,时间戳转字符串)用法。

涉及的函数

  1. date_format(date, format)
  2. unix_timestamp()
  3. str_to_date(str, format)
  4. from_unixtime(unix_timestamp, format)

使用及示例

  • 时间转字符串(date->string)
 select date_format(now(), '%Y-%m-%d');

输出结果:2019-03-23

 

  • 时间转时间戳(date->long)
select unix_timestamp(now());

输出结果:1553346922

 

  • 字符串转时间(string->date)
select str_to_date('2019-03-23', '%Y-%m-%d');

输出结果:2019-03-23 00:00:00

 

  • 字符串转时间戳(date->long)
select unix_timestamp('2019-03-23');

输出结果:1553270400

 

  • 时间戳转时间(long->date)
select from_unixtime(1553270400);

输出结果:2019-03-23 00:00:00

 

  • 时间戳转字符串(long->string)
select from_unixtime(1553270400,'%Y-%m-%d');

输出结果:2019-03-23

 

 

转自于:https://www.cnblogs.com/wangyongwen/p/6265126.html

 


ITZOO版权所有丨如未注明 , 均为原创丨转载请注明来自IT乐园 ->Mysql中时间 Date和字符串,时间戳间相互转换
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址