...

View Full Version : date formatting from mysql



ziggy1621
01-02-2008, 03:56 PM
i'm currently pulling the field 'departDate' from mysql as YYYY-MM-DD (it is saved as date format). I'm using the following function to reformat it for display:



<?
function reformat_date($date, $format){
$output = date($format, strtotime($date));
return $output;
}
$departDate= reformat_date($departDate, "F j, Y");
?>

The problem is, when i'm trying to display multiple items using the following, I get an error that it "Cannot redeclare reformat_date() (previously declared.."

this is what I have:



while ( $aRow = mysql_fetch_array( $aQResult ) )
{
$departDate=$aRow["departDate"];

////reformat date
function reformat_date($date, $format){
$output = date($format, strtotime($date));
return $output;
}
$departDate= reformat_date($departDate, "F j, Y");

print($departDate);

}
mysql_free_result( $aQResult );




thanks in advance for any help

anarchy3200
01-02-2008, 04:05 PM
Try moving the code for your function outside of the while loop.

i.e.


////reformat date
function reformat_date($date, $format){
$output = date($format, strtotime($date));
return $output;
}
while ( $aRow = mysql_fetch_array( $aQResult ) )
{
$departDate=$aRow["departDate"];

$departDate= reformat_date($departDate, "F j, Y");

print($departDate);

}
mysql_free_result( $aQResult );



What that was doing before was trying to declare the function for every loop but it only needs to be done once.

ziggy1621
01-02-2008, 04:35 PM
such a beautiful thing....

THanks

Fumigator
01-02-2008, 04:36 PM
You can also use MySQL's date_format() function to do the formatting for you, (assuming the column is a date type column) thus avoiding the need for an additional PHP function.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum