Formatting an SQL timestamp with PHP

后端 未结 6 546
無奈伤痛
無奈伤痛 2020-12-01 16:15

I have a mySQL database with a timestamp field. It currently only has one entry while I\'m testing, it is

2010-02-20 13:14:09

I am pulling

6条回答
  •  鱼传尺愫
    2020-12-01 16:42

    For starters, the php date() function is expecting seconds as the second variable. So that accounts for why your date is displaying wrong. Check this source on that issue.

    Which then provides us the answer to the problem, to get PHP to format the date from a SQL timestamp correctly, we just change the query a tad...

    SELECT author, `when`
    

    Change it to...

    SELECT author, UNIX_TIMESTAMP(`when`)
    

    Then use the PHP date function, with the variable that is storing the result of that above SQL query.

提交回复
热议问题