如何在 MySQL 中将时间戳转换为日期时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5362874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:12:11  来源:igfitidea点击:

How to convert timestamp to datetime in MySQL?

mysql

提问by compile-fan

How to convert 1300464000to 2011-03-18 16:00:00in MySQL?

如何在 MySQL 中转换13004640002011-03-18 16:00:00

回答by Richard Tuin

Use the FROM_UNIXTIME()function in MySQL

FROM_UNIXTIME()在 MySQL 中使用该函数

Remember that if you are using a framework that stores it in milliseconds (for example Java's timestamp) you have to divide by 1000to obtain the right Unix time in seconds.

请记住,如果您使用的框架以毫秒为单位存储它(例如 Java 的时间戳),您必须除以 1000才能获得正确的 Unix 时间(以秒为单位)。

回答by Kingshuk Deb

DATE_FORMAT(FROM_UNIXTIME(`orderdate`), '%Y-%m-%d %H:%i:%s') as "Date" FROM `orders`

This is the ultimate solution if the given date is in encoded format like 1300464000

如果给定日期采用编码格式,则这是最终解决方案 1300464000

回答by ksvendsen

To answer Janus Troelsen comment

回答 Janus Troelsen 评论

Use UNIX_TIMESTAMPinstead of TIMESTAMP

使用UNIX_TIMESTAMP而不是TIMESTAMP

SELECT from_unixtime( UNIX_TIMESTAMP(  "2011-12-01 22:01:23.048" ) )

The TIMESTAMP function returns a Date or a DateTime and not a timestamp, while UNIX_TIMESTAMP returns a unix timestamp

TIMESTAMP 函数返回 Date 或 DateTime 而不是时间戳,而 UNIX_TIMESTAMP 返回 unix 时间戳

回答by Dilraj Singh

You can use

您可以使用

select from_unixtime(1300464000,"%Y-%m-%d %h %i %s") from table;

For in details description about

有关详细说明

  1. from_unixtime()
  2. unix_timestamp()
  1. from_unixtime()
  2. unix_timestamp()

回答by Leo

SELECT from_unixtime( UNIX_TIMESTAMP(fild_with_timestamp) ) from "your_table"
This work for me

SELECT from_unixtime( UNIX_TIMESTAMP(fild_with_timestamp) ) from "your_table"
这对我有用