This is a repost of something I put into a thread that was marked RESOLVED around the time I submitted this.
I'm trying to correctly format a time, given a date and a time in the form of HH:MM:SS (pulled from a MySQL query). The time is the third element in each record, so this code is executed when i equals 2.
echo "<table border=\"1\">";
$fields = mysql_num_fields($result);
while ($array = mysql_fetch_array($result))
{
echo "<tr>";
//time is second record, and I'm only using the second, third and fourth records of all selected. May change in the future
for ($i=2; $i <= 4; $i++)
{
if ($i == 2)
{
//time is in HH:MM:SS format, need to explode it
$j = explode(":", $array[i]);
//used for debugging, nothing printed when uncommented.
//print "$j[0] $j[1] $j[2]";
//want to display in 12h format with no leading zeroes in the hour.
//x is an exploded date passed to the function from another PHP file.
$newtime = date("g:i:s A", mktime($j[0], $j[1], $j[2], $x[0], $x[1], $x[2], -1));
echo "<td>$newtime</td>";
}
//if not a time record, echo the contents of that field as text.
else
{
echo "<td>$array[$i]</td>";
}
if ( $i < $fields ) echo "<td></td> ";
}
echo "</tr>";
}
echo "</table>";
When this code is executed, the time displays as "6:59:59 PM" for all entries selected in the query.