How To Process MySQL Timestamp in PHP

Many of us do not realize that MySQL's Timestamp is not the timestamp which PHP date() understands. In fact if you try to use MySQL timestamp directly in php date() function then you will get totally wrong dates. There is a simple way to use MySQL timestamp data in php.

First you need to modify the SQL query to use MySQL's UNIX_TIMESTAMP() function. So if your SQL looks like:
SELECT author, `when` …

then convert it to:
SELECT author, UNIX_TIMESTAMP(`when`) …

Then use PHP's date() function to convert the unix timestamp to appropriate display format like this:
date("F j, Y, g:i a", $row[1])

where $row[1] contains the timestamp column value.
Enhanced by Zemanta

Share/Bookmark

No comments: