Date format - help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Date format - help

Post 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?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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'];
Post Reply