MySQL
Unix timestamp
date conversion
database query
programming tips

Convert Unix timestamp into human readable date using MySQL

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

sql
SELECT FROM_UNIXTIME(1633024800);

This returns:

 
2021-10-01 00:00:00

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:
sql
  SELECT FROM_UNIXTIME(1633024800, '%Y-%m-%d %H:%i:%s');

From Datetime to Unix Timestamp

Conversely, UNIX_TIMESTAMP() converts a date to a Unix timestamp. Here's a basic example:

sql
SELECT UNIX_TIMESTAMP('2021-10-01 00:00:00');

This returns:

 
1633024800

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:

sql
SELECT DATE_FORMAT(FROM_UNIXTIME(1633024800), '%W, %M %d, %Y');

This outputs:

 
Friday, October 01, 2021

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():

sql
SELECT CONVERT_TZ(FROM_UNIXTIME(1633024800), '+00:00', '-04:00');

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:
sql
  SELECT FROM_UNIXTIME(-86400);

Outputs:

 
  1969-12-31 00:00:00

SQL Table for Summary

Below is a table summarizing the core functions and examples discussed:

FunctionPurposeExample UsageExample Output
FROM_UNIXTIME()Converts Unix timestamp to readable formatSELECT FROM_UNIXTIME(1633024800);2021-10-01 00:00:00
UNIX_TIMESTAMP()Converts date to Unix timestampSELECT UNIX_TIMESTAMP('2021-10-01 00:00:00');1633024800
DATE_FORMAT()Formats datetime with custom patternSELECT DATE_FORMAT(NOW(), '%W, %M %d, %Y');Wednesday, March 23, 2022
CONVERT_TZ()Converts datetime across time zonesSELECT 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.


Course illustration
Course illustration

All Rights Reserved.