Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change date format 1

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
My dates stored in my database look like 2006-09-02 etc, but I want to change the date format when it gets loaded into a dropdown, ideally to look like 2-SEP-06.

Should I convert the query table field of Orderdate within a loop before putting items in a list or is there a way to intercept the code below $row[Orderdate]? Thanks

echo "<option size =10 </option>";
if(mysql_num_rows($result))
{
while($row = mysql_fetch_assoc($result))
{
echo "<option>$row[Orderdate]</option>";
}
}
else {
echo "<option>No Data Present</option>";
}

echo '</select name>';
 
Sorry, just to clear any confusion, the date format wanted is only for list display, ie not wanting to convert database storage format. Thanks
 
Code:
echo "<option>".date("j-M-Y", strtotime($row['Orderdate']))."</option>";
 
Thanks very much, that fixed it a treat. Have a star and good weekend.
 
Having column alignment problems now I am adding other columns? I have added a field/column called Caption,its an irregular length. It causes an alignment problem to column following it. Is there a way to cycle thtough each column and set a space value against the longest of each column, so its ready for when it goes in the list. Modified code :

foreach ($_rows as $row):
if (strlen($row['Ordernumber']) > $max['Ordernumber']) {$max['Ordernumber'] = strlen($row['Ordernumber']);}
if (strlen($row['Orderdate']) > $max['Orderdate']) {$max['Orderdate'] = strlen($row['Orderdate']);}
if (strlen($row['Caption']) > $max['Caption']) {$max['Caption'] = strlen($row['Caption']);}
if (strlen($row['Description']) > $max['Description']) {$max['Description'] = strlen($row['Description']);}
$rows[] = $row;
endforeach;

$contents = "<select style=\"font-family:monospace;\" name=\"selectbox\" multiple size=\"25\">\r\n";
foreach ($rows as $row):
$contents .= "<option value=\"{$row['Value']}\">";
$contents .= sprintf("%-'#".($max['Ordernumber'] + 2)."s", $row['Ordernumber']);
$contents .= sprintf("%-'#". ($max['Orderdate'] + 2)."s", $row['Orderdate']);
$contents .= sprintf("%-'#". ($max['Caption'] + 2)."s", $row['Caption']);
$contents .= sprintf("%-'#". ($max['Description'] + 2)."s", $row['Description']);
$contents .= "</option>\r\n";
endforeach;

Many thanks
 
Solved, stpid me. The line:
$contents .= sprintf("%-'#". ($max['Caption'] + 2)."s", $row['Caption']);

I changed the value of 2 to 5, that solved it for the maximum column length. Looks good, thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top