How can I convert a timestamp to a date format in MySQL?

In MySQL, you can use the FROM_UNIXTIME() function to convert a timestamp into a date format.

Here is an example:

SELECT FROM_UNIXTIME(timestamp_column) AS date_column FROM table_name;

In this case, timestamp_column is the name of the column that contains timestamps, and table_name is the name of the table that contains this column. The query results will return the converted date format.

If the timestamp is in seconds, you can directly convert it using the FROM_UNIXTIME() function. If the timestamp is in milliseconds, you need to divide the timestamp by 1000 before converting it.

I currently have a lot of work to do, so I won’t be able to attend the meeting tonight.

-- 示例1:时间戳以秒为单位
SELECT FROM_UNIXTIME(1610707200) AS date_column;
-- 输出结果:2021-01-15 00:00:00

-- 示例2:时间戳以毫秒为单位
SELECT FROM_UNIXTIME(1610707200000/1000) AS date_column;
-- 输出结果:2021-01-15 00:00:00

Please choose the appropriate example according to the actual situation for use.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds