Home >Database >Mysql Tutorial >How to Convert MySQL Unix Timestamps to Datetime Strings?

How to Convert MySQL Unix Timestamps to Datetime Strings?

Barbara Streisand
Barbara StreisandOriginal
2024-11-27 08:42:11651browse

How to Convert MySQL Unix Timestamps to Datetime Strings?

MySQL Timestamp to Datetime Conversion

In MySQL, converting a Unix timestamp (like 1300464000) to a datetime string (such as '2011-03-18 16:00:00') can be achieved using the FROM_UNIXTIME() function.

Usage:

SELECT FROM_UNIXTIME(timestamp) AS datetime_string;

Example:

To convert the timestamp 1300464000 to a datetime string, you would use the following query:

SELECT FROM_UNIXTIME(1300464000) AS datetime_string;

This query would return the following datetime string:

'2011-03-18 16:00:00'

Note for Millisecond Timestamps:

If you're working with a timestamp that is stored in milliseconds (as is common in some programming frameworks), remember to divide by 1000 to obtain the correct Unix timestamp in seconds. For example, if the timestamp is 1234567890123, the following query would be used:

SELECT FROM_UNIXTIME(1234567890123 / 1000) AS datetime_string;

The above is the detailed content of How to Convert MySQL Unix Timestamps to Datetime Strings?. 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