Date format in MySQL

Discussion in 'PHP' started by Mike, Aug 22, 2005.

  1. Mike

    Mike New Member

    Joined:
    Oct 19, 2004
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.go4expert.com/
    In mySql DB date is stored as a yyyy-mm-dd but i want to show it as dd-mm-yyyy.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    Use of SQL's prime reason is whatever you store is independent of what you can retrieve (format). That is view can be changed to whaterver format we wish to get from the stored data.

    Now when you store the date its like yyyy-mm-dd and now to retrieve in dd-mm-yyyy you can just use the following SQL so that you dont need to do anything fancy in PHP

    PHP:
    $sql "SELECT DATE_FORMAT(date_column_name, '%d-%m-%Y') AS new_date FROM table_name WHERE...."
    Also you can see the complete Description of DATE_FORMAT function here http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
     
  3. Mike

    Mike New Member

    Joined:
    Oct 19, 2004
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.go4expert.com/
  4. Mike

    Mike New Member

    Joined:
    Oct 19, 2004
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.go4expert.com/
    OOPs Type error. Thanks.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice