SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How do I format a date selected from mysql?

    Hi all,
    In the code below, wk1 is a date in format yyyy-mm-dd. I want to display it in the format dd mmm (e.g. 09 Nov).
    Can anyone help me?

    Thanks

    <?php include 'dbconnect.php';
    // Request wk1
    $result = @mysql_query("SELECT wk1 FROM wk1 WHERE id=1");
    if (!$result) {
    exit('<p>Error performing query: ' . mysql_error() . '</p>');
    }
    // Display the date in a paragraph
    while ($row = mysql_fetch_array($result)) {
    echo $row['wk1'];
    }
    ?>

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,423
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    look at the date() function in PHP or date_format() in MySQL

  3. #3
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but I can't make it work. Do you know?

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT DATE_FORMAT(wk1, '%d %b') as foo FROM wk1 WHERE id=1

  5. #5
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,108
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Or you can use PHP's functions on a UNIX timestamp.

    If it's in the db as 'yyyy-mm-dd'
    and you can get those values as $yyyy, $mm and $dd

    Code:
    $unix_timestamp = $mktime(0, 0, 0, $mm, $dd, $yyyy);
    date("F j, Y", $unix_timestamp);
    That way there's only one call to the db and PHP can do the rest of the work, perhaps if you wanted to display the date in a lot of different forms the PHP way would be more flexible.


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
  •