How to manipulate a value in a loop
Posted: Fri Oct 19, 2012 9:25 pm
I'm an amateur coder at best. I'm working on an internal website that will do selects on a db to retrieve information. One of the returned fields contains a timestamp of the form unixtime.microseconds. I'd like to convert that to human readable mm/dd/yyyy h
s. I've tried several things, but everything I've tried breaks the displayed result. The results are returned in a table with titles in the top row followed by the data returned from the db in rows.
Note: I have a decent understanding of programming basics. I steal most of my code from the web and then adapt it to my use plus reading copious amounts of documentation trying to figure all this out.
Here's the working code:
If it matters, the third column is the one with the timestamp.micros format. I've tried an if else loop something like this:
But it didn't work the way I expected at all. It returned one empty row.
I also tried an if for the specific row:
I'm sure this is something simple for you everyday coders, but it's got me stumped.
Note: I have a decent understanding of programming basics. I steal most of my code from the web and then adapt it to my use plus reading copious amounts of documentation trying to figure all this out.
Here's the working code:
Code: Select all
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
echo "</table>\n";
mysql_free_result($result);Code: Select all
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell) {
$pattern='/\d{10}\.\d{6}/';
$timestamp=preg_match($pattern,$cell);
$HRDate=date(n/j/Y h:m:s,$timestamp);
if($HRDate) {
echo "<td>$HRDate</td>";
}else{
echo "<td>$cell</td>";
}
}
echo "</tr>\n";
}
echo "</table>\n";
mysql_free_result($result);I also tried an if for the specific row:
Code: Select all
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell) {
if($row==2) {
$HRDate=date(n/j/Y h:m:s);
echo "<td>$HRDate</td>";
}else{
echo "<td>$cell</td>";
}
}
echo "</tr>\n";
}
echo "</table>\n";
mysql_free_result($result);