1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

change date format in php / mysql query

Discussion in 'PHP' started by scoopy82, Apr 20, 2009.

  1. #1
    I have been attempting to change the date format that is stored in my database so that it displays properly and validates for my RSS feed.

    My current code is:
    $c = mysqli_connect('localhost', 'user', 'pass', 'database') or die(mysqli_error($c));
    $q = mysqli_query($c, 'SELECT * FROM articles ORDER BY id DESC LIMIT 20') or die(mysqli_error($c));
    
    while($r = mysqli_fetch_assoc($q))
    
    	{
    
    		echo '<pubDate>'.$r['DateAct'].'</pubDate>';
    
    	}
    Code (markup):
    Where "articles" is the table of my database and "DateAct" is the stored date. The date currently appears like so:
    2009-04-20 21:19:59
    Code (markup):
    I was attempting to add something like, "DATE_FORMAT('DateAct', '%W %D %M %Y')" into my query... but failed ;)
     
    scoopy82, Apr 20, 2009 IP
  2. joep1978

    joep1978 Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Don't put quotes around the field name otherwise the database treats it like a string
     
    joep1978, Apr 20, 2009 IP
  3. darren884

    darren884 Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try doing "SELECT DATE_FORMAT(DateAct, '%W %D %M %Y') FROM" etc...
     
    darren884, Apr 21, 2009 IP
  4. scoopy82

    scoopy82 Active Member

    Messages:
    838
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    70
    #4
    Tried this:
    $q = mysqli_query($c, 'SELECT ID, Category_Name, Author, Title, DATE_FORMAT(DateAct, '%W %D %M %Y') FROM articles ORDER BY id DESC LIMIT 20') or die(mysqli_error($c));
    Code (markup):
    and a bunch of other variations (including REPLACE) but end up with errors such as:
    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
    Code (markup):
    That line works using DateAct like so:
    $q = mysqli_query($c, 'SELECT ID, Category_Name, Author, Title, DateAct FROM articles ORDER BY id DESC LIMIT 20') or die(mysqli_error($c));
    Code (markup):
    But it produces the date in the following format:
    2009-04-20 21:19:59
    Code (markup):
    Which is exactly how it is stored in that database column.
     
    scoopy82, Apr 22, 2009 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    You have to escape: DATE_FORMAT(DateAct, \'%W %D %M %Y\') inside string.
    Regards
     
    koko5, Apr 22, 2009 IP
  6. scoopy82

    scoopy82 Active Member

    Messages:
    838
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    70
    #6
    Almost ;) That got rid of the error... but it also got rid of the date in the RSS feed.
     
    scoopy82, Apr 22, 2009 IP
  7. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #7
    Than you can get formatted date as $r['FOO']
    :)
     
    koko5, Apr 22, 2009 IP
  8. scoopy82

    scoopy82 Active Member

    Messages:
    838
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    70
    #8
    That works... but it is posting the current date instead of what is saved in the database ?
     
    scoopy82, Apr 22, 2009 IP
  9. SiteTalkZone

    SiteTalkZone Peon

    Messages:
    243
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    SiteTalkZone, Apr 22, 2009 IP