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

How to Convert Unix Timestamps to MySQL Datetimes?

DDD
Release: 2024-12-01 12:57:10
Original
767 people have browsed it

How to Convert Unix Timestamps to MySQL Datetimes?

Converting Timestamps to Datetimes in MySQL

For a given timestamp, such as 1300464000, the task is to obtain its equivalent datetime representation in MySQL. It is crucial to note that timestamps are represented as the number of seconds since Epoch (January 1, 1970 UTC) in Unix systems.

To achieve this conversion in MySQL, leverage the FROM_UNIXTIME() function. For instance, to convert the provided timestamp (1300464000) to datetime, use the following query:

SELECT FROM_UNIXTIME(1300464000);
Copy after login

This query will return the datetime value as '2011-03-18 16:00:00'.

Note: For timestamps stored in milliseconds, which is common in Java, remember to divide the value by 1000 before using it in FROM_UNIXTIME(). This ensures you work with Unix time in seconds, enabling the accurate conversion to datetime.

The above is the detailed content of How to Convert Unix Timestamps to MySQL Datetimes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template