php mysql distinct year custom timestamp

I have a set of records in mysql that have a timestamp that looks like this

2010/04/14

How can I select the distinct year of all these records using a mysql query?
webrhpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gamebitsCommented:
$year = "2010";

SELECT * FROM tablename WHERE fieldname LIKE "$year%";
0
webrhpAuthor Commented:
Ideally, how can this be done in a while loop? I've got records for many years
0
gamebitsCommented:
$year = array(

'2000',
'2001',
'2002',
'2003',
'2004',
'2005',

);

foreach($year as $value)
{
  $sql = "SELECT * FROM tablename WHERE fieldname LIKE '$value%'";
  $result = mysql_query($sql);

   while($data = mysql_fetch_array($result)){

      echo "HERE DO YOUR HTML OF HOW YOU WANT TO DISPLAY THE INFO";

      }
}

to display the information use the format  "".$data['dbfieldname'].""
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

hieloCommented:
I think what you are asking for is:
SELECT DISTINCT Year(`yourDateField`) FROM yourTable
0
webrhpAuthor Commented:
hielo: I get an empty array from that


SELECT DISTINCT (YEAR('date')) FROM payments

while($timeperiod = mysql_fetch_array($get_earnings)) {
	
print_r($timeperiod); 

shows:

Array ( [0] => [(YEAR('date'))] => )

Open in new window

0
webrhpAuthor Commented:
gamebits: that was a structure i was using originally. the reason for the question was to automate the process. i wouldn't want to manually update that array.
0
webrhpAuthor Commented:
Just figured it out:


$get_earnings = mysql_query("SELECT DISTINCT LEFT(date,4) FROM payments ORDER BY LEFT(date,4) DESC");

while($timeperiod = mysql_fetch_array($get_earnings)) {

print_r($timeperiod); // Array ( [0] => 2010 [LEFT(date,4)] => 2010 )

print $timeperiod[0]; // 2010

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hieloCommented:
>>hielo: I get an empty array from that
>>SELECT DISTINCT (YEAR('date')) FROM payments
That's NOT what I gave you. I surrounded the field with backticks (usually, it is the character on the same key as the ~ character). Instead of backticks, YOU used apostrophes. In MySQL VALUES are sorrounded with apostrophes, but
database, tables, and/or field names are surrounded with backticks.

SELECT DISTINCT YEAR(`date`) FROM payments
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.