Home >Database >Mysql Tutorial >How Can I Convert a Unix Timestamp to a MySQL Datetime?

How Can I Convert a Unix Timestamp to a MySQL Datetime?

Barbara Streisand
Barbara StreisandOriginal
2024-11-25 21:02:15293browse

How Can I Convert a Unix Timestamp to a MySQL Datetime?

Converting Timestamps to Datetime in MySQL

When working with timestamps in MySQL, converting them to their datetime equivalents can be useful. Consider the case where you have a timestamp such as 1300464000 and want to convert it to its corresponding datetime format.

Solution:

MySQL provides the FROM_UNIXTIME() function to accomplish this conversion. To convert 1300464000 to datetime, use the following query:

SELECT FROM_UNIXTIME(1300464000) AS datetime;

Output:

datetime
2011-03-18 16:00:00

Additional Note:

If the timestamp is stored in milliseconds (as is the case with Java's timestamp), you must divide it by 1000 before using FROM_UNIXTIME() to obtain the correct Unix time in seconds.

The above is the detailed content of How Can I Convert a Unix Timestamp to a MySQL Datetime?. 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