Home > Database > Mysql Tutorial > How to Convert Epoch Timestamps to Human-Readable Dates in MySQL?

How to Convert Epoch Timestamps to Human-Readable Dates in MySQL?

Mary-Kate Olsen
Release: 2024-10-26 22:25:03
Original
902 people have browsed it

How to Convert Epoch Timestamps to Human-Readable Dates in MySQL?

Converting Epoch Timestamps to Human-Readable Dates in MySQL

In MySQL, an epoch timestamp is a numeric representation of a date and time. It is a count of milliseconds since the Unix epoch, which is January 1, 1970, at 00:00:00 UTC.

To convert an epoch timestamp to a human-readable date, you can use the from_unixtime() function. This function takes an epoch timestamp as its argument and returns a string containing the date and time in a specified format.

The following example shows how to convert an epoch timestamp of 1389422614485 to a human-readable date:

<code class="mysql">SELECT from_unixtime(1389422614485);</code>
Copy after login

This will return the following string:

2014-01-11 12:13:34
Copy after login
Copy after login

Note that the epoch timestamp in the example has a millisecond precision. For epoch timestamps that represent seconds, you can use the following syntax:

<code class="mysql">SELECT from_unixtime(1389422614);</code>
Copy after login

This will return the following string:

2014-01-11 12:13:34
Copy after login
Copy after login

Update July 2020: As of MySQL 8.0, the floor function is no longer necessary when working with milliseconds:

<code class="mysql">SELECT from_unixtime(1594838230234/1000);</code>
Copy after login

This will return:

2020-07-15 18:37:10.2340
Copy after login

The above is the detailed content of How to Convert Epoch Timestamps to Human-Readable Dates in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template