Skip navigation
Currently Being Moderated

Formatting mySQL date in a dynamic field

Jul 27, 2009 7:06 PM

Hi all.. I've seen many articles on here about how to INSERT dates to the mySQL table, but I need to know how to format it into MM/DD/YYYY when the form dynamically populates from an existing record.  Just using a regular DW8 Recordset behavior for this...

Thanks-

 
Replies
  • Currently Being Moderated
    Jul 27, 2009 8:57 PM   in reply to AdrianLe

    In ColdFusion you would use the date format function

     

    #DateFormat(CreateODBCDate(yourDate), "dd/mm/yyyy")#

     

    For more information on this do a Google search on: ColdFusion date format function

     

    In PHP

     

    date("Y/m/d")


    For more info on this do a Google search on: php date format function



    --
    Lawrence   *Adobe Community Expert*
    www.Cartweaver.com
    Complete Shopping Cart Application for
    Dreamweaver, available in ASP, PHP and CF
    www.twitter.com/LawrenceCramer

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 28, 2009 4:36 AM   in reply to AdrianLe

    AdrianLe wrote:

     

    I need to know how to format it into MM/DD/YYYY when the form dynamically populates from an existing record.

    Use the MySQL DATE_FORMAT() function and assign the result to an alias in your SQL.

     

    For example, let's say you have a date stored in a field called start. The following would format the date in your desired format, and present it in the recordset results as start_date:

    SELECT DATE_FORMAT(start, '%m/%d/%Y') AS start_date,
    another_field, yet_another
    FROM my_table
    

    There's a full list of the formatting characters in the MySQL reference manual (follow the link above).

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 28, 2009 4:40 AM   in reply to David_Powers

    By the way, the answer to this question was in Formatting dates stored in MySQL in the Dreamweaver FAQ.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 28, 2009 8:41 AM   in reply to AdrianLe

    That makes no difference. Just write your SQL so that it includes the DATE_FORMAT() and alias.

     

    If you're being incredibly lazy, you can do this:

    SELECT *, DATE_FORMAT(start) AS start_date
    FROM my_table
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 29, 2009 4:30 AM   in reply to AdrianLe

    Interesting.. both of your suggestions return errors claiming that there are too few arguments, resulting in an empty query.

    Yes, there were too few arguments in the second example. It should be this:

    SELECT *, DATE_FORMAT(start, '%m/%d/%Y') AS start_date
    FROM my_table

    Make sure you use single quotes around the '%m/%d/%Y'.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 29, 2009 8:41 AM   in reply to AdrianLe

    That should work. What happens when you use SELECT joindate FROM member?

     

    The alternative way to do it is to create a small PHP function like this:

    function format_mysql($date) {
      $parts = explode('-', $date);
      if (is_array($parts) && count($parts == 3)) {
        return "$parts[1]/$parts[2]/$parts[0]";
      } else {
        return 'Date not available';
      }
    }
    

    Then display the result from your recordset like this:

    <?php echo format_mysql($row_recordsetName['joindate']); ?>
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 29, 2009 9:36 AM   in reply to AdrianLe

    There's something wrong with your code somewhere. I have just run the following test code, and it displays '02/01/1993', exactly as I expected it would.

    <?php
    function format_mysql($date) {
      $parts = explode('-', $date);
      if (is_array($parts) && count($parts == 3)) {
        return "$parts[1]/$parts[2]/$parts[0]";
      } else {
        return 'Date not available';
      }
    }
    $test = '1993-02-01';
    echo format_mysql($test);
    ?>
    
    
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points