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

How to Convert MySQL Unix Timestamps to Datetime Strings?

Barbara Streisand
Release: 2024-11-27 08:42:11
Original
650 people have browsed it

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;
Copy after login

Example:

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

SELECT FROM_UNIXTIME(1300464000) AS datetime_string;
Copy after login

This query would return the following datetime string:

'2011-03-18 16:00:00'
Copy after login

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;
Copy after login

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template