MySQL
timestamp conversion
SQL query
date formatting
database management

Convert timestamp to date in MySQL query

Master System Design with Codemia

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

MySQL provides a host of functions for handling date and time values, including those for converting timestamps into readable date formats. This task is common in various database operations, especially when dealing with Unix timestamps, which are single integers representing the number of seconds since January 1, 1970. This article will explore the techniques and functions used in MySQL to convert timestamps to dates, with examples to support these explanations.

Converting Timestamps to Date in MySQL

Understanding MySQL Date and Time Functions

MySQL offers several built-in functions to manage and manipulate date and time data types. The primary function used for converting timestamps to date formats is FROM_UNIXTIME(). When handling these data types, it's crucial to understand the following key concepts:

  • Unix Timestamp: A way to track time as a running total of seconds since the epoch, i.e., 00:00:00 UTC on 1 January 1970.
  • DateTime Format: The standard format used to represent date and time in MySQL, usually YYYY-MM-DD HH:MM:SS.

The FROM_UNIXTIME() Function

The FROM_UNIXTIME() function takes a Unix timestamp and converts it to a standard date and time format. Here’s the basic syntax:

sql
FROM_UNIXTIME(unix_timestamp);

Example Usage

Consider the task of converting a Unix timestamp into a human-readable date format. Let’s work through an example where we convert the Unix timestamp 1609459200 – which represents midnight on January 1, 2021:

sql
SELECT FROM_UNIXTIME(1609459200);

The output will be:

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

Formatting Dates

FROM_UNIXTIME can also accept a second argument to specify a custom format for the output date.

sql
FROM_UNIXTIME(unix_timestamp, format);

Format Example

To convert the timestamp to only display the date in YYYY-MM-DD format:

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

Expected Output:

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

Combining with Other Functions

It’s possible to use FROM_UNIXTIME() in combination with other MySQL functions for more flexible date manipulation.

Example: Calculating Age

Suppose you wish to calculate a person's age from a timestamp representing their date of birth. Use the TIMESTAMPDIFF() function:

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

Here, the timestamp 631152000 represents a date of birth on January 1, 1990.

Handling Time Zones

MySQL utilizes the server's default time zone when converting timestamps to dates. If you need conversions to a specific time zone, you can manage this using CONVERT_TZ().

Time Zone Example

Example converting time to Eastern Standard Time (EST):

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

Key Concepts in a Table

Below is a summary table of the key functions and concepts discussed:

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')

Additional Considerations

  • Data Type Limits: Beware of the limits of the timestamp data type. MySQL’s TIMESTAMP data type range is from ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC.
  • Precision: If you need fractional seconds, consider using MySQL 5.6 or later, which supports DATETIME(fsp) for fractional second precision.

Understanding how to effectively convert timestamps into dates in MySQL can significantly enhance your database management and reporting capabilities. By mastering these functions and methods, you ensure that date and time data are both meaningful and formatted correctly for your applications.


Course illustration
Course illustration

All Rights Reserved.