Format date in MySQL SELECT as ISO 8601

前端 未结 7 2125
攒了一身酷
攒了一身酷 2020-12-05 17:14

I\'m trying to grab the date from my database in a standard timestamp and display it as ISO 8601. I\'m unable to easily do it in PHP so I\'m trying to do it in my SELECT st

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 17:42

    DATE_FORMAT only works on MySQL date columns, not timestamps.

    A UNIX timestamp is an integer containing the number of seconds since Jan 1, 1970 UTC. To format this as an ISO 8601 date you need to use the FROM_UNIXTIME() function instead.

    FROM_UNIXTIME takes the same format strings as DATE_FORMAT, so to format a column named 'created' you'd:

    SELECT created /* e.g. 1288799488 */ , 
           FROM_UNIXTIME(created,'%Y-%m-%dT%TZ') /* e.g. 2010-11-03T08:51:28Z */
    FROM table_name
    

提交回复
热议问题