Results 1 to 11 of 11

Thread: [RESOLVED] Format date for report

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Resolved [RESOLVED] Format date for report

    i have this and its in YYYY-MM-DD format now.

    Code:
    <td><font size="1" face="Arial, Helvetica, sans-serif"><?php echo $callindate; ?></font></td>
    i need to format the callindate just for the report in the echo command to MM/DD/YYYY

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Format date for report

    You could use string processing...
    PHP Code:
    function changeDate($d){
      
    $dateParts explode('-',$d);
      return 
    $dateParts[1].'/'.$dateParts[2].'/'.$dateParts[0];
    }
    echo 
    changeDate($callindate); 
    ...or the DateTime object (available in PHP 5.2+)...
    PHP Code:
    function changeDate($d){
      
    $dt = new DateTime($d);
      return 
    $dt->format('m/d/Y');
    }
    echo 
    changeDate($callindate); 
    I usually store dates as a timestamp, as that's often the easiest to reformat using just the date() function.
    Last edited by SambaNeko; Oct 9th, 2010 at 09:56 PM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: Format date for report

    Samba,

    i tried

    Code:
    function changeDate($d){
      $dateParts = explode('-',$d);
      return $dateParts[1].'/'.$dateParts[2].'/'.$dateParts[0];
    }
    <td><font size="1" face="Arial, Helvetica, sans-serif"><?php echo changeDate($callindate); ?></font></td>
    and i get an error
    Fatal error: Call to undefined function changedate() in /data/21/1/14/99/1992262/user/2178205/htdocs/ReportAllByUnit.php on line 231

    i also tried the other function as well with the same result. I must be doing something wrong.

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Format date for report

    if what you pasted is where you put the function definition, you seemed to have defined it in the HTML. if this is the case, you can't do that. functions are PHP code and must be defined in PHP. it would be best to put function definitions at the very top, or very bottom, of your script. or, put them in an include file and then include them as you need them.
    PHP Code:
    <?php
      
    function changeDate($d){
        
    $dateParts explode('-',$d);
        return 
    $dateParts[1].'/'.$dateParts[2].'/'.$dateParts[0];
      }
    ?>
    <td><font size="1" face="Arial, Helvetica, sans-serif"><?php echo changeDate($callindate); ?></font></td>
    Like Archer? Check out some Sterling Archer quotes.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: Format date for report

    kows,

    i doing this
    Code:
    <?php
      function changeDate($d){
        $dateParts = explode('-',$d);
        return $dateParts[1].'/'.$dateParts[2].'/'.$dateParts[0];
      }
    ?>
    <td><font size="1" face="Arial, Helvetica, sans-serif"><?php echo changeDate($callindate); ?></font></td>
    and i get this error

    Code:
    Fatal error: Cannot redeclare changedate() (previously declared in /data/21/1/14/99/1992262/user/2178205/htdocs/ReportAllDesc.php:214) in /data/21/1/14/99/1992262/user/2178205/htdocs/ReportAllDesc.php on line 214
    line 214 is the "function changedate($d){

    i dont have the function anywhere else

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Format date for report

    oh. then you did define it properly the first time. but, then there shouldn't be any reason for the script to complain the function doesn't exist. remove one of the declarations and try again?
    Like Archer? Check out some Sterling Archer quotes.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: Format date for report

    this did it

    Code:
    <?php
    if(!function_exists('changeDate'))
    {
    
      function changeDate($d){
        $dateParts = explode('-',$d);
        return $dateParts[1].'/'.$dateParts[2].'/'.$dateParts[0];
      }
     }
    ?>
    
    <td><center><font size="1" face="Arial, Helvetica, sans-serif"><?php echo changeDate($callindate); ?></font></center></td>

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Format date for report

    .. that doesn't do anything, though, unless you're defining the function twice for some reason. just remove the second definition of the function; you only need to define it once.
    Like Archer? Check out some Sterling Archer quotes.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: [RESOLVED] Format date for report

    but where is the second declaration? what i have works but not sure why exactly.

  10. #10
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Format date for report

    I don't know, I don't have your code. press CTRL+F in your text editor and find changeDate. it has to be there somewhere.
    Like Archer? Check out some Sterling Archer quotes.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: [RESOLVED] Format date for report

    I have a lot of php file with functions in them. it must be in one of those somewhere. thanks again for you help.

Posting Permissions

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



Click Here to Expand Forum to Full Width