MySQL
timestamp conversion
SQL query
date formatting
database management

Convert a Unix timestamp to a date in a MySQL query

Codemia Knowledge Hub ·

Use FROM_UNIXTIME(). It takes a Unix timestamp (seconds since 00:00:00 UTC on 1 January 1970) and returns a datetime in MySQL's standard YYYY-MM-DD HH:MM:SS format.

Two concepts matter here:

  • Unix timestamp: a single integer that tracks time as a running total of seconds since the epoch, 00:00:00 UTC on 1 January 1970.
  • DateTime format: the standard way MySQL represents date and time, usually YYYY-MM-DD HH:MM:SS.

FROM_UNIXTIME() is the built-in function that converts the first into the second. The basic syntax:

sql
FROM_UNIXTIME(unix_timestamp);

For example, 1609459200 represents midnight on January 1, 2021:

sql
SELECT FROM_UNIXTIME(1609459200);

Output:

 
1+---------------------------+
2| FROM_UNIXTIME(1609459200) |
3+---------------------------+
4| 2021-01-01 00:00:00       |
5+---------------------------+

Getting only the date with a format string

FROM_UNIXTIME accepts a second argument that sets the output format:

sql
FROM_UNIXTIME(unix_timestamp, format);

To show only the date in YYYY-MM-DD form:

sql
SELECT FROM_UNIXTIME(1609459200, '%Y-%m-%d');

Expected output:

 
1+---------------------------------------+
2| FROM_UNIXTIME(1609459200, '%Y-%m-%d') |
3+---------------------------------------+
4| 2021-01-01                            |
5+---------------------------------------+

Using the result in date arithmetic

The converted value works with other date functions. For example, to calculate an age from a date-of-birth timestamp, use TIMESTAMPDIFF():

sql
SELECT TIMESTAMPDIFF(YEAR, FROM_UNIXTIME(631152000), CURDATE()) AS Age;

Here 631152000 is a date of birth of January 1, 1990.

Converting to a specific time zone

MySQL converts timestamps using the server's default time zone. To get a specific zone, wrap the result in CONVERT_TZ(). This example converts to Eastern Standard Time (EST):

sql
SELECT CONVERT_TZ(FROM_UNIXTIME(1609459200), '+00:00', '-05:00');

Function reference

Concept/FunctionDescriptionExample
Unix TimestampSeconds since 1970-01-01 00:00:00 UTC.1609459200
FROM_UNIXTIME()Converts Unix timestamp to YYYY-MM-DD HH:MM:SS.FROM_UNIXTIME(1609459200)
FROM_UNIXTIME() with formatConverts to a specific date format.FROM_UNIXTIME(1609459200, '%Y-%m-%d')
TIMESTAMPDIFF()Calculates the difference between two dates.TIMESTAMPDIFF(YEAR, date1, date2)
CONVERT_TZ()Converts date and time from one time zone to another.CONVERT_TZ(date, '+00:00', '-05:00')

TIMESTAMP range and fractional seconds

  • Range: MySQL's TIMESTAMP data type runs from '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
  • Precision: For fractional seconds, use MySQL 5.6 or later, which supports DATETIME(fsp) for fractional second precision.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design