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

发布于 2024-11-14 20:17:14 字数 83 浏览 1 评论 0原文

是否有 MySQL 函数可用于将 Unix 时间戳转换为人类可读的日期?我有一个字段用于保存 Unix 时间,现在我想添加另一个字段用于人类可读的日期。

Is there a MySQL function which can be used to convert a Unix timestamp into a human readable date? I have one field where I save Unix times and now I want to add another field for human readable dates.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

巷子口的你 2024-11-21 20:17:14

使用 FROM_UNIXTIME()

SELECT
  FROM_UNIXTIME(timestamp) 
FROM 
  your_table;

另请参阅:MySQL 文档FROM_UNIXTIME()

Use FROM_UNIXTIME():

SELECT
  FROM_UNIXTIME(timestamp) 
FROM 
  your_table;

See also: MySQL documentation on FROM_UNIXTIME().

素染倾城色 2024-11-21 20:17:14

其他答案中缺少的并且不直接明显的是 from_unixtime 可以采用第二个参数来指定格式,如下所示:

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

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:

SELECT
  from_unixtime(timestamp, '%Y %D %M %H:%i:%s')
FROM 
  your_table
赴月观长安 2024-11-21 20:17:14

我认为您正在寻找的是 FROM_UNIXTIME()

I think what you're looking for is FROM_UNIXTIME()

初见终念 2024-11-21 20:17:14

需要特定时区的 Unix 时间戳吗?

如果您可以快速访问 mysql cli,这里有一个简单的说明:

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

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

'MST' 替换为您想要的时区。我住在亚利桑那州

Need a unix timestamp in a specific timezone?

Here's a one liner if you have quick access to the 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. I live in Arizona ???? thus the conversion from UTC to MST.

戒ㄋ 2024-11-21 20:17:14

为什么要费心将字段保存为可读?只有我们 AS

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

编辑:抱歉,我们以毫秒而不是秒存储所有内容。修好了。

Why bother saving the field as readable? Just us 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.

赤濁 2024-11-21 20:17:14

您可以使用 DATE_FORMAT 函数。 这里是一个包含示例的页面,以及可用于选择不同日期组件的模式。

You can use the DATE_FORMAT function. Here's a page with examples, and the patterns you can use to select different date components.

狼性发作 2024-11-21 20:17:14

简单易行的方法:

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

Easy and simple way:

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

一个人的旅程 2024-11-21 20:17:14

因为我发现这个问题没有意识到,mysql总是将时间存储在UTC的时间戳字段中,但会在本地时区显示(例如phpmyadmin),我想添加我的发现。

我有一个自动更新的last_modified字段,定义为:

`last_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

用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`

在任何星座中,UNIX_TIMESTAMP和'as UTC'总是以UTC时间显示。

运行两次,第一次不设置 time_zone。

Since I found this question not being aware, that mysql always stores time in timestamp fields in UTC but will display (e.g. phpmyadmin) in local time zone I would like to add my findings.

I have an automatically updated last_modified field, defined as:

`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

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.

Run this twice, first without setting the time_zone.

烟花易冷人易散 2024-11-21 20:17:14

如果您想转换时间并以特定格式显示数据,您可以使用此字符串。

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

convert_tz 添加到 date_format 字符串。 %m/%d/%y月/日/年
您可以在这里找到所有特定格式 https://www.w3schools.com/sql/func_mysql_date_format。 ASP

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. the %m/%d/%y being month/day/year.
you can find all the specific formats here https://www.w3schools.com/sql/func_mysql_date_format.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文