How To Process MySQL Timestamp in PHP
By Angsuman Chakraborty, Gaea News NetworkSaturday, May 27, 2006
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.
Tags: Fact
![]() yossi greenbaum |
December 24, 2009: 4:41 pm
I wish I could have found this article a couple of hours ago. It’s awesome!!!! |
![]() Karthik |
June 1, 2009: 8:42 pm
There’s a much easier way Don’t bother using UNIX_TIMESTAMP(`when`) You can always do this: $sPHPDate = date(’whatever format u want’,strtotime($aRowFromDbResult['when'])); strtotime is exceptionally clever it can parse a wide variety of date formats including MySQL’s default date and timestamp format |
![]() Carlos Jerez |
March 20, 2008: 3:06 pm
Aca va una clase que les puede ayudar un poco /** if ($unixTimestamp===null) return date(FechasMysql::_formatoTimeStampMysql,$unixTimestamp); /** if ($unixTimestamp===null) return date(FechasMysql::_formatoHoraMysql,$unixTimestamp); /** if ($unixTimestamp===null) return date(FechasMysql::_formatoFechaMysql,$unixTimestamp); /** if ($unixTimestamp===null) if ($opcionString==’F') } /** $unixTimestamp=FechasMysql::UnixTimeStamp($mysqlTimeStamp); /** $arrayTimestamp=explode(FechasMysql::_separadorFechaHoraMysql, $mysqlTimeStamp); return $unixTimesTamp = mktime($arrayHora[0],$arrayHora[1],$arrayHora[2],$arrayFecha[1],$arrayFecha[2],$arrayFecha[0]); private static function ConfigurarZona() } |
g