Home >Database >Mysql Tutorial >How Can MySQL\'s FROM_UNIXTIME() Function Convert Unix Timestamps to Human-Readable Dates?

How Can MySQL\'s FROM_UNIXTIME() Function Convert Unix Timestamps to Human-Readable Dates?

Barbara Streisand
Barbara StreisandOriginal
2024-11-28 12:52:14126browse

How Can MySQL's FROM_UNIXTIME() Function Convert Unix Timestamps to Human-Readable Dates?

Converting Unix Timestamps to Human-Readable Dates with MySQL

Many databases use Unix timestamps to store date and time information. However, for user-facing applications, a human-readable date format is often preferred. This article explains how to use the MySQL FROM_UNIXTIME() function to easily convert Unix timestamps into a more readable format.

FROM_UNIXTIME() Function

The FROM_UNIXTIME() function takes a Unix timestamp as its argument and returns a human-readable date string. The format of the returned date string can be customized using a variety of format specifiers.

Example:

SELECT
  FROM_UNIXTIME(timestamp) 
FROM 
  your_table;

This query will convert all Unix timestamps in the "timestamp" column of the "your_table" table to human-readable dates.

Format Specifiers:

The following format specifiers can be used to customize the output format of the FROM_UNIXTIME() function:

  • %Y: Year (e.g., 2021)
  • %m: Month (e.g., 03)
  • %d: Day of the month (e.g., 01)
  • %H: Hour (e.g., 12)
  • %i: Minute (e.g., 22)
  • %s: Second (e.g., 05)

Examples of Formatted Output:

  • FROM_UNIXTIME(timestamp, '%Y-%m-%d'): YYYY-MM-DD (e.g., 2021-03-01)
  • FROM_UNIXTIME(timestamp, '%Y-%m-%d %H:%i:%s'): YYYY-MM-DD HH:MM:SS (e.g., 2021-03-01 12:22:05)

Additional Resources:

  • [MySQL documentation on FROM_UNIXTIME()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_from-unixtime)

The above is the detailed content of How Can MySQL\'s FROM_UNIXTIME() Function Convert Unix Timestamps to Human-Readable Dates?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn