简体   繁体   English

使用 MySQL 将 Unix 时间戳转换为人类可读的日期

[英]Convert Unix timestamp into human readable date using MySQL

Is there a MySQL function which can be used to convert a Unix timestamp into a human readable date?是否有 MySQL function 可用于将 Unix 时间戳转换为人类可读的日期? I have one field where I save Unix times and now I want to add another field for human readable dates.我有一个字段可以保存 Unix 次,现在我想为人类可读日期添加另一个字段。

Use FROM_UNIXTIME() :使用FROM_UNIXTIME()

SELECT
  FROM_UNIXTIME(timestamp) 
FROM 
  your_table;

See also: MySQL documentation on FROM_UNIXTIME() .另请参阅: MySQL 文档FROM_UNIXTIME()

What's missing from the other answers (as of this writing) and not directly obvious is that from_unixtime can take a second parameter to specify the format like so:其他答案中缺少的(截至撰写本文时)并不直接明显的是from_unixtime可以采用第二个参数来指定格式,如下所示:

SELECT
  from_unixtime(timestamp, '%Y %D %M %H:%i:%s')
FROM 
  your_table

I think what you're looking for is FROM_UNIXTIME()我认为您正在寻找的是FROM_UNIXTIME()

Need a unix timestamp in a specific timezone?需要特定时区的 unix 时间戳吗?

Here's a one liner if you have quick access to the mysql cli:如果您可以快速访问 mysql cli,这里有一个衬里:

mysql> select convert_tz(from_unixtime(1467095851), 'UTC', 'MST') as 'local time';

+---------------------+
| local time          |
+---------------------+
| 2016-06-27 23:37:31 |
+---------------------+

Replace 'MST' with your desired timezone.'MST'替换为您想要的时区。 I live in Arizona thus the conversion from UTC to MST.我住在亚利桑那州,因此从 UTC 转换为 MST。

Why bother saving the field as readable?为什么要麻烦将字段保存为可读? Just us AS只有AS

SELECT theTimeStamp, FROM_UNIXTIME(theTimeStamp) AS readableDate
               FROM theTable
               WHERE theTable.theField = theValue;

EDIT: Sorry, we store everything in milliseconds not seconds.编辑:抱歉,我们以毫秒而不是秒为单位存储所有内容。 Fixed it.解决它。

You can use the DATE_FORMAT function.您可以使用 DATE_FORMAT function。 Here 'sa page with examples, and the patterns you can use to select different date components.是带有示例的页面,以及您可以用于 select 不同日期组件的模式。

Easy and simple way:简单易行的方法:

select from_unixtime(column_name, '%Y-%m-%d') from table_name

Since I found this question not being aware, that mysql always stores time in timestamp fields in UTC but will display (eg phpmyadmin) in local time zone I would like to add my findings.由于我发现这个问题没有意识到,mysql 总是将时间存储在 UTC 的时间戳字段中,但会在本地时区显示(例如 phpmyadmin)我想添加我的发现。

I have an automatically updated last_modified field, defined as:我有一个自动更新的 last_modified 字段,定义为:

`last_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Looking at it with phpmyadmin, it looks like it is in local time, internally it is UTC用phpmyadmin看,好像是当地时间,内部是UTC

SET time_zone = '+04:00'; // or '+00:00' to display dates in UTC or 'UTC' if time zones are installed.
SELECT last_modified, UNIX_TIMESTAMP(last_modified), from_unixtime(UNIX_TIMESTAMP(last_modified), '%Y-%c-%d %H:%i:%s'), CONVERT_TZ(last_modified,@@session.time_zone,'+00:00') as UTC FROM `table_name`

In any constellation, UNIX_TIMESTAMP and 'as UTC' are always displayed in UTC time.在任何星座中,UNIX_TIMESTAMP 和“as UTC”始终以 UTC 时间显示。

Run this twice, first without setting the time_zone.运行两次,首先不设置 time_zone。

If you would like to convert time AND display the data in a specific format you can use this string.如果您想转换时间并以特定格式显示数据,您可以使用此字符串。

date_format(convert_tz(from_unixtime(TIMESTAMP), 'UTC', 'DESIRED TZ'), '%m/%d/%y')

where you add convert_tz to a date_format string.您将convert_tz添加到 date_format 字符串的位置。 the %m/%d/%y being month/day/year . %m/%d/%ymonth/day/year you can find all the specific formats here https://www.w3schools.com/sql/func_mysql_date_format.asp你可以在这里找到所有的具体格式https://www.w3schools.com/sql/func_mysql_date_format.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM