RE: [PHP] MySQL Timestamp to Date conversion

From: Date: Sat, 05 Aug 2000 00:47:09 +0000
Subject: RE: [PHP] MySQL Timestamp to Date conversion
Groups: php.general 
Request: Send a blank email to php-general+get-10227@lists.php.net to get a copy of this message
The docs are a little confusing. The date() function does format a timestamp, but not a MySQL timestamp; instead, it deals with a UNIX epoch-based timestamp. I wrote the function below to parse a MySQL timestamp and transform it into a UNIX timestamp, at which point you'll be able to run the date() function on it. You'd change your code as follows: $formatdate = date("M/D/Y H:i:s", mysql_to_unix_time( $timestamp )); Here's the function (the foreach loop isn't strictly necessary, but makes it more readable): /////////////////////////////////////////////////////////// // mysql_to_unix_time: converts a 14-digit MySQL ////////// // timestamp to a UNIX timestamp ////////////////////////// /////////////////////////////////////////////////////////// /* $datetime should be a number in the format yyyymmddhhmmss Returns the date's UNIX timestamp */ function mysql_to_unix_time ($datetime) { // Convert to string so we can reliably parse it settype($datetime, 'string'); // Break the number up into its components (yyyymmddhhmmss) // storing results in the array matches eregi('(....)(..)(..)(..)(..)(..)',$datetime,$matches); // Pop the first element off the matches array. The first // element is not a match, but the original string, which // we don't want. array_shift ($matches); // Transfer the values in $matches into labeled variables foreach (array('year','month','day','hour','minute','second') as $var) { $$var = array_shift($matches); } return mktime($hour,$minute,$second,$month,$day,$year); } --Bruce Bruce Christensen Intel Corporation brucex.r.christensen@intel.com -----Original Message----- From: Strider Centaur [mailto:strider@scifi-fantasy.com] Sent: Friday, August 04, 2000 5:32 PM To: php-general@lists.php.net Subject: [PHP] MySQL Timestamp to Date conversion I read at the PHP.NET site in the documentation that this should format a MySQL timestamp into a readable date format. Anyone have any ideas? Heres a code segment that illistrates what Im trying to accomplish. <?PHP // //CREATE TABLE SomeTable ( timestamp TIMESTAMP, NAME char(60) ); // $q = "SELECT timestamp FROM SomeTable"; $r = mysql_query( $q ); while ( $row = mysql_fetch_array( $r ) ) { $formatdate = date("M/D/Y H:i:s", time( $timestamp )); // THIS NOT WORKING? print "$formatdate<BR>"; } ?> -- Strider Centaur HTTP://www.Scifi-Fantasy.com " It is my observation that unless you really understand the issues, you are hardly in a position to criticize. Nearly all Linux users have used Windows, but very few Windows users have used Linux. " -- Me -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#10227) next »