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:
For example, 1609459200 represents midnight on January 1, 2021:
Output:
Getting only the date with a format string
FROM_UNIXTIME accepts a second argument that sets the output format:
To show only the date in YYYY-MM-DD form:
Expected output:
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():
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):
Function reference
| Concept/Function | Description | Example |
| Unix Timestamp | Seconds 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 format | Converts 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
TIMESTAMPdata 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.
Related reading
- convert_tz returns null
- Convert Unix timestamp into human readable date using MySQL
- converting database from mysql to mongoDb
- Copy SQL Server data from one server to another on a schedule
- Copy table without copying data
- Copy values from one column to another in the same table
- Copy/duplicate database without using mysqldump
- Copying one table to another in DynamoDB
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.