Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    156
    Thanks
    8
    Thanked 1 Time in 1 Post

    date formatting from mysql

    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:

    PHP Code:
    <?
    function reformat_date($date$format){
        
    $output date($formatstrtotime($date));
        return 
    $output;
    }
      
    $departDatereformat_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:

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

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

    print(
    $departDate);
                
            }
                    
    mysql_free_result$aQResult ); 
    thanks in advance for any help

  • #2
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try moving the code for your function outside of the while loop.

    i.e.
    PHP Code:
    ////reformat date 
    function reformat_date($date$format){ 
    $output date($formatstrtotime($date)); 
    return 
    $output

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

    $departDatereformat_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.
    Mike

  • Users who have thanked anarchy3200 for this post:

    ziggy1621 (01-02-2008)

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    156
    Thanks
    8
    Thanked 1 Time in 1 Post
    such a beautiful thing....

    THanks

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •