How to convert timestamp to date MySQL

旧街凉风 提交于 2019-12-10 17:40:01

问题


I'm following a guide on YouTube on how to make an Image Uploading website, by phpacademy, great guide. However, I'm trying to extend it a bit by displaying it in a human-readable way.

At the moment the albums.timestamp is saved on the database as UNIX_TIMESTAMP() and results in 1348372089 for example. Now, if I want to convert these numbers into a regular date and time, such as 23-09-2012 05:00 (European standard), how should I proceed?

I have tried DATE(albums.timestamp) and CONVERT(albums.timestamp, 120), unix_timestamp(albums.timestamp) as well as CAST(120 as albums.timestamp), but none of them works.


回答1:


Use FROM_UNIXTIME(str,format). http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime

The second parameter will let you format the date string to your liking.

Formatting characters:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format




回答2:


FROM_UNIXTIME(albums.timestamp) works though!



来源:https://stackoverflow.com/questions/12549685/how-to-convert-timestamp-to-date-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!