Convert Unix timestamp into human readable date using MySQL
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In working with databases and applications, it's common to encounter timestamps in various formats. One ubiquitous format is the Unix timestamp, which represents the number of seconds elapsed since the "Unix Epoch" (00:00:00 UTC on 1 January 1970). While Unix timestamps are useful for storage and computation, they are not human-readable. Fortunately, MySQL provides built-in functions to convert Unix timestamps into more understandable date and time formats. This article explores how to perform these conversions in MySQL, with examples and explanations to aid understanding.
Understanding Unix Timestamps
A Unix timestamp is a single number representing the number of seconds since the Unix Epoch. It accounts for various adjustments such as leap seconds to adapt to the Earth’s rotation. Unix timestamps are time zone agnostic as they’re ideally stored in UTC, representing a single point in time universally.
MySQL Functions to Convert Unix Timestamps
MySQL offers several functions to work with dates and times. Two key functions for converting Unix timestamps into human-readable formats are FROM_UNIXTIME() and UNIX_TIMESTAMP().
From Unix Timestamp to Date
The FROM_UNIXTIME() function is used to convert a Unix timestamp to a datetime value in 'YYYY-MM-DD HH:MM:SS' format. Here's how it is used:
This returns:
Explanation:
- The function takes a Unix timestamp as an input and outputs the corresponding date and time in GMT format.
- Optionally, you can format the output using a format specifier:
From Datetime to Unix Timestamp
Conversely, UNIX_TIMESTAMP() converts a date to a Unix timestamp. Here's a basic example:
This returns:
Explanation:
- If no argument is given,
UNIX_TIMESTAMP()returns the current Unix timestamp. - With a date string argument, it returns the Unix timestamp corresponding to that date and time.
Formatting Human-Readable Dates
MySQL’s date-formatting capabilities can customize output further. The DATE_FORMAT() function allows conversion of a date into various string formats. Combining it with FROM_UNIXTIME() offers elaborate formatting:
This outputs:
Time Zone Handling
While Unix timestamps represent a global universal time, conversion functions aren't directly timezone aware. However, MySQL supports timezone conversion using CONVERT_TZ():
This adapts the date-time to a different timezone—in this case, converting from UTC to EDT.
Error Handling
MySQL will handle out-of-range error scenarios, such as timestamps before the epoch start:
- Negative timestamps are resolved to dates before 1970:
Outputs:
SQL Table for Summary
Below is a table summarizing the core functions and examples discussed:
| Function | Purpose | Example Usage | Example Output |
FROM_UNIXTIME() | Converts Unix timestamp to readable format | SELECT FROM_UNIXTIME(1633024800); | 2021-10-01 00:00:00 |
UNIX_TIMESTAMP() | Converts date to Unix timestamp | SELECT UNIX_TIMESTAMP('2021-10-01 00:00:00'); | 1633024800 |
DATE_FORMAT() | Formats datetime with custom pattern | SELECT DATE_FORMAT(NOW(), '%W, %M %d, %Y'); | Wednesday, March 23, 2022 |
CONVERT_TZ() | Converts datetime across time zones | SELECT CONVERT_TZ(NOW(), '+00:00', '-04:00'); | 2022-03-23 14:30:00 |
Conclusion
MySQL effectively facilitates the conversion of Unix timestamps into human-readable formats and vice versa. Whether it involves timezone adaptation, custom formatting, or dealing with special cases such as negative timestamps, MySQL's powerful features extend beyond basic conversion. Understanding these functions enhances the handling of temporal data, providing essential value in developing robust applications that can effortlessly manage date-time transformations.
Related reading
- converting database from mysql to mongoDb
- 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
- Copying third party databases into central mySQL and keeping mySQL up to date

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.