SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    date format within mysqli_query(

    Hello all, I'm very new to using PHP and just need a little help with formating the date via MYSQL. Here is the code that I am getting an error on...

    $result = mysqli_query($link, 'SELECT id, name, rank, DATE_FORMAT(opened, '%d-%m-%Y'), active');

    Thank you for taking time to read my post and any suggestions would be greatly appreciated. --Ben

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought that maybe I would clarify my post, I seem to get a lot views, but no replys yet.
    If I use the following code, I get no error:

    $result = mysqli_query($link, 'SELECT id, name, rank, opened, active');

    And "opened" in MYSQL is a DATE column. The date that is displayed is YYYY-MM-DD. I was hoping to get the date to display as follows... d-m-Y.
    When I use the following code, I get an error:

    $result = mysqli_query($link, 'SELECT id, name, rank, DATE_FORMAT(opened, '%d-%m-%Y'), active');

    I'm not sure if I'm using DATE_FORMAT incorectly or if there is another method? Thanks again for reading my post and any help/suggestions would be greatly appreciated. --Ben

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    i don't do php and i figured a php developer would reply

    but since no one did, i can tell you that your error is caused by your quotes

    $result = mysqli_query($link, 'SELECT id, name, rank, DATE_FORMAT(opened, '%d-%m-%Y'), active');

    the quote for your date format string is terminating the mysqli_query parameter too soon

    try swapping single for double quotes -- i'd be more specific, but i don't do php
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    r937,

    Thank you very much for the suggestion, the cause made perfect sense in your post. I used double quotes in the date format and had to follow it with AS afterwards--it worked perfectly with no errors. Here is the final code...

    $result = mysqli_query($link, 'SELECT id, name, rank, DATE_FORMAT(opened, "%d-%m-%Y") AS opened, active');

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. --Ben


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
  •