Ryan_w Posted January 7, 2008 Share Posted January 7, 2008 Hey Guys, Need a bit of help with a problem pulling data from a mysql database. I have timestamps stored in an integer field in mysql. I am pulling those rows out in a foreach statement, and I need to run the date function to output that timestamp in a human readable format in a table. When I pull that field with the rest of the row it is entered into the array as a string. I have tried the normal ways (intvar, (int)) to convert this to an integer to be used in the date command and none seem to do so properly. How can I go about converting that string into a proper integer without changing the timestamp? Basic jist of what I am trying to do: $sql = ("SELECT * FROM database WHERE field = '" . $variable . "';"); $result = mysql_query($sql); foreach (mysql_fetch_assoc($result) as $row) { echo(date('format string', $row["timestamp"])); } Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/ Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 echo(date('D F d Y', $row["timestamp"])); What does $row["timestamp"] contain? Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432678 Share on other sites More sharing options...
mike1313 Posted January 7, 2008 Share Posted January 7, 2008 Let the manual be your best friend http://www.php.net/manual/en/function.date.php Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432685 Share on other sites More sharing options...
Ryan_w Posted January 7, 2008 Author Share Posted January 7, 2008 $row["timestamp"] contains a unix timestamp inserted by another application....but php pulls it as a string into the array instead of an integer....and date() won't have it. Warning: date() expects parameter 2 to be long, string given in /root/php/test.php on line 19 If I echo the entire array it shows the timestamp file....but when I echo($row["timestamp"]; it converts the string into a set of alpha numeric chars and that is what the date function sees. Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432690 Share on other sites More sharing options...
mike1313 Posted January 7, 2008 Share Posted January 7, 2008 So, is it being stored in the database as an integer? Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432695 Share on other sites More sharing options...
Ryan_w Posted January 7, 2008 Author Share Posted January 7, 2008 Yes Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432696 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 Try it with a single quote or no quote, $row['timestamp'] I usually use a Object Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432697 Share on other sites More sharing options...
Ryan_w Posted January 7, 2008 Author Share Posted January 7, 2008 Nope....it still converts it to an alpha/numeric string with single quotes Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432707 Share on other sites More sharing options...
mike1313 Posted January 7, 2008 Share Posted January 7, 2008 try foreach (mysql_fetch_array($result) as $row) { echo(date('format string', $row[timestamp])); } Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432712 Share on other sites More sharing options...
Ryan_w Posted January 7, 2008 Author Share Posted January 7, 2008 I still get multiple errors about date wanting a long int....because it is interpreting it as a string I am only a couple months into using PHP....I would love to use an object....but I really don't want to spend 5 hours learning a whole new concept right now to fix this :/ There has to be a simple way to either tell it to pull this as an integer like it is stored in the database or to convert it properly I would think. Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432715 Share on other sites More sharing options...
revraz Posted January 7, 2008 Share Posted January 7, 2008 This is what I do, if this doesn't work, then it's not stored as a INT while ($row = mysql_fetch_object ($result)) echo date('m/d/Y-H:i',$row->timestamp); Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432719 Share on other sites More sharing options...
mike1313 Posted January 7, 2008 Share Posted January 7, 2008 In the database is it being stored as an INT or are you trying to store it as TIMESTAMP? Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432722 Share on other sites More sharing options...
Ryan_w Posted January 7, 2008 Author Share Posted January 7, 2008 revraz, That did the trick...thanks for the help guys. Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432723 Share on other sites More sharing options...
mike1313 Posted January 7, 2008 Share Posted January 7, 2008 This is what I do, if this doesn't work, then it's not stored as a INT while ($row = mysql_fetch_object ($result)) echo date('m/d/Y-H:i',$row->timestamp); If you left out the extra set of () that may have been your problem Quote Link to comment https://forums.phpfreaks.com/topic/84875-mysql-stringinteger-problem-solved/#findComment-432725 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.