Page 1 of 1

Date format - help

Posted: Sun Jun 12, 2005 5:55 am
by crazytopu
Hi,

I am into some problem with the date format of mysql. The following code lists all my dates currently stored in "date" column in table - test.

Code: Select all

<?php

require_once('DbConnector.php');

//creating a database connector object
$connector = new DbConnector();

$sql='select date from test order by date desc';
$result=$connector->query($sql);
while ($row = $connector->fetchArray($result)) {
echo $row[date].'<br>'; 
}

?>
The format it shows my dates is -


Code: Select all

2005-12-19
2005-07-13
2005-06-30
2005-06-12
2005-06-07
2005-04-05
2005-02-17
2005-02-15
2004-06-10
2003-06-20
But I want the date to be displayed in the following format:

Code: Select all

Month Name in full (space) date,(space) Year in 4 digit
(example - October 9, 2004 )
I also want to retrieve just the distinct year from the date column.

Which functions should i consult to get these things done? I tried this for the first thing:

Code: Select all

<?php

require_once('DbConnector.php');

//creating a database connector object
$connector = new DbConnector();

$sql='select date from test order by date desc';
$result=$connector->query($sql);
while ($row = $connector->fetchArray($result)) {
echo $row[date].'<br>'; 
echo date($row[date],'F d,Y').'<br>';
}

?>
No luck.

Could you help?

Posted: Sun Jun 12, 2005 6:45 am
by anjanesh

Code: Select all

$sql = "
SELECT 
DATE_FORMAT(`date`,'%M %d, %Y') AS fdate,
DATE_FORMAT(`date`,'%Y') AS JustYear,
FROM `test` ORDER BY `date` DESC
";

$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
echo $row['fDate'].'<br/>'.$row['JustYear'];