Forum Moderators: coopster

Message Too Old, No Replies

Converting MySql timestamp and display in php

Timestamp is converted using UNIX_TIMESTAMP. Need to add to select. now.

         

lovduv

12:39 pm on Oct 17, 2008 (gmt 0)

10+ Year Member



I can successfully convert the MySQL timestamp for php using this:

$result=mysql_query("SELECT UNIX_TIMESTAMP(submitted) AS FORMATED_TIME FROM Data");
$FORMATED_TIME=mysql_result($result,0,"FORMATED_TIME");
$date=date("m/d/y - g:ia",$FORMATED_TIME);

This will successfully print out the date in the correct format with echo, but I am unsure of how to roll that into my loop below. I am new to php and not sure if there is way to simply push the code above into my select statement and make it a part of $query? If not, how would I add the converted timestamp into this line? $output .= '<td align="center">' . $row['submitted'] .'</td>';

This is my entire function:

function ShowList(){

$output = '<table border="1" width="700" cellpadding="5" style="border-collapse: collapse; border-color: black">
<tr bgcolor="#ADAAAA" bordercolor="black">
<th>ID#</th>
<th>Employer</th>
<th>Name</th>
<th>Month</th>
<th>Year</th>
<th>Submitted</th>
<th width="76">Action</th>
</tr>';

// run the query, order by Name
$query = mysql_query("select from Data order by Employer asc, Name asc");

// loop all the records
while($row = mysql_fetch_assoc($query)){
$output .= '<tr bordercolor="black" bgcolor="#C4F5BF">';
$output .= '<td align="center">' . $row['ID'] .'</td>';
$output .= '<td align="center">' . $row['Employer'] . '</td>';
$output .= '<td align="center">' . $row['Name'] .'</td>';
$output .= '<td align="center">' . $row['Month'] .'</td>';
$output .= '<td align="center">' . $row['Year'] .'</td>';
$output .= '<td align="center">' . $row['submitted'] .'</td>';
$output .= '<td valign="top" width="96"><a href="?action=edit&ID=' . $row['ID'] . '">Edit</a> - ';

// need to add slashes as we're dealing with javascript here
$output .= '<a href="javascript:checkDelete(\'' . addslashes($row['Name']) . '\',\'' . addslashes($row['Month']) . '\',\'' . addslashes($row['Year']) . '\',' . $row['ID'] . ');">Delete</a></td>';
$output .= '</tr>';
}
$output .= '</table>';

return $output;
}

mooger35

10:19 pm on Oct 17, 2008 (gmt 0)

10+ Year Member



If I understand you correctly you want to format a date from your database in your table?

First your query needs to be like this:

// run the query, order by Name
$query = mysql_query("select * from Data order by Employer asc, Name asc");

Then when echoing the date:

$output .= '<td align="center">' . date("m/d/y - g:ia",strtotime($row['submitted'])) .'</td>';

lovduv

1:51 am on Oct 18, 2008 (gmt 0)

10+ Year Member



Wonderful! Thank you sooo much, works perfect! : )