Results 1 to 5 of 5

Thread: php mysql date echo

  1. #1
    Join Date
    Mar 2009
    Posts
    42
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default php mysql date echo

    Hi, I really need some help, I have been trying to solve this problem for a while now, and i just can't get it.

    I have a simple mysql data base with a few tables on it, including a Date field.

    Problem is when I echo the date on my php page its displayed yyyy-mm-dd, and I need it to be displayed dd-mm-yy.

    Here is the code I am using to echo the tables and feilds.

    <?php

    $query="select * from `news flash` ORDER BY Date ASC"; // query string stored in a variable ASC.

    $rt=mysql_query($query); // query executed

    echo mysql_error(); // if any error is there that will be printed to the screen
    while($nt=mysql_fetch_array($rt)){
    echo "<h3>$nt[Title]</h3> <p>$nt[Date]</p> <p>$nt[Content]</p>";

    }

    ?>

    hope it makes sence!

    Thank you for your help and suggestions

    JT
    Last edited by john0611; 03-02-2009 at 01:27 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try:
    PHP Code:
    <?php

    function changeToDifferentDate($old){
      
    $arr_date explode("-"$old);
      return 
    mktime(000$arr_date[1], $arr_date[2], $arr_date[0]);
    }
    $query="select * from `news flash` ORDER BY Date ASC"// query string stored in a variable ASC.

    $rt=mysql_query($query); // query executed 

    echo mysql_error(); // if any error is there that will be printed to the screen
    while($nt=mysql_fetch_array($rt)){
    $date date("d-m-y"changeToDifferentDate($nt['Date']));
    echo 
    "<h3>{$nt['Title']}</h3> <p>{$date}</p> <p>{$nt['Content']}</p>"

    }

    ?>
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    john0611 (03-02-2009)

  4. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Do not use a function for this. Change your query to what is below.

    SQL doesn't make you write custom functions to format dates.

    $query="SELECT *, DATE_FORMAT(Date, '%d-%m-%Y') as Date FROM `news flash` ORDER BY Date ASC ";

  5. The Following User Says Thank You to JasonDFR For This Useful Post:

    john0611 (03-02-2009)

  6. #4
    Join Date
    Mar 2009
    Posts
    42
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    FANTASTIC!! works great!. Thank you both for all your help!

  7. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Glad to help you! Your welcome!
    It seems your topic is solved... Please set the status to resolved.. To do this:
    Go to your first post ->
    Edit your first post ->
    Click "Go Advanced" ->
    Then in the drop down next to the title, select "RESOLVED"
    Jeremy | jfein.net

  8. The Following User Says Thank You to Nile For This Useful Post:

    john0611 (03-02-2009)

Bookmarks

Posting Permissions

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