Home >Database >Mysql Tutorial >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);
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!