Jump to content

MySQL date to day(th) month year


Flames

Recommended Posts

i need to get the values for j,S, M and y from the database.

 

I'm not too sure what you're asking for, but maybe this will help:

 

list($y,$m,$d) = explode("-",$date);

 

It returns the year, month, and day values from a yyyy-mm-dd date

Link to comment
Share on other sites

this is the php from my code:

<?php
$sql = "SELECT * FROM News ORDER BY id DESC LIMIT 1";
$query = mysql_query($sql);
while($news = mysql_fetch_assoc($query))
{
$message = $news['message'];
$poster = $news['poster'];
$date = $news['date'];
$date2 = date("jS M, y", $date);
}
echo <<<EOT
$message, posted by <span class="poster">$poster</span>, on the <span class="style7">$date2</span>
EOT;
?>

it always returns 31st december 69, even though the date is 2008-10-19

 

@AndyB i dont get how i would put that in to the date function, otherwise that would just say 19 10 2008 which isnt how i want it.

 

@PFMaBiSmAd i cant do that because im selecting other columns as well.

Link to comment
Share on other sites

so in my code would it just be

<?php
$sql = "SELECT *, DATE_FORMAT(date) AS %d %b, %y FROM News ORDER BY id LIMIT 1";
$query = mysql_query($sql);
while($news = mysql_fetch_assoc($query))
{
$message = $news['message'];
$poster = $news['poster'];
$date = $news['date'];
}
echo <<<EOT
$message, posted by <span class="poster">$poster</span>, on the <span class="style7">$date</span>
EOT;
?>

Link to comment
Share on other sites

You might want to read the information and the examples at that link. The syntax is - DATE_FORMAT(date,format). Date is your date column name and format is the format string that gives you the results you want.

 

The AS some_name syntax assigns an alias name so that you can refer to the resultant value by that name, which you probably already knew from your basic mysql reference book.

Link to comment
Share on other sites

Since mind reading seems to be working today over the Internet, I'll answer your next question before you even ask it in this thread -

 

All strings must be enclosed in quotes (single-quotes are used in mysql). All six examples at that link show the format string enclosed in single-quotes. How you managed to read that information and leave out that part of the syntax is a mystery.

Link to comment
Share on other sites

i managed to get my code working thanks.

 

I didnt notice that, i tried my code without and i thought it was a MySQL error hence the reason i made a new topic, do you know anywhere where i could find help with the AS some_name because i've never used it before but i managed to get it working by a complete guess of $news['some_name'].

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.