开发者

Convert Unix timestamp into human readable date using MySQL

开发者 https://www.devze.com 2023-03-11 11:00 出处:网络
Is there a MySQL function which can be used to convert a Unix timestamp into a human readable date? I have o开发者_开发技巧ne field where I save Unix times and now I want to add another field for huma

Is there a MySQL function which can be used to convert a Unix timestamp into a human readable date? I have o开发者_开发技巧ne field where I save Unix times and now I want to add another field for human readable dates.


Use FROM_UNIXTIME():

SELECT
  FROM_UNIXTIME(timestamp) 
FROM 
  your_table;

See also: MySQL documentation on 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:

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消