Current location: Hot Scripts Forums » Programming Languages » PHP » Extracting the year from the date


Extracting the year from the date

Reply
  #1 (permalink)  
Old 03-29-04, 01:58 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Extracting the year from the date

I am up dating a page and db. Before I had the month + day in one colum and the year in a nother colum of my database. This made it quite easy to use in sorting by year.

I thought to myself today thats not the " correct " way to do it, I sould be using 0000-00-00.

But I have a small problem - I am populating a dropdown list of years to pick and I cant get it to work now.

Any help would be apreciated.

Anthony

PHP Code:

$query "SELECT DISTINCT year FROM photos ORDER BY year";

  
$result mysql_query($query)
       or die (
"Couldn't execute query."); 
Worked OK with the old db.

PHP Code:

$query "SELECT DISTINCT date ("Y") FROM photos ORDER BY date ("Y") ";

  
$result mysql_query($query)
       or die (
"Couldn't execute query."); 
I thought this would be right but it dose not work. Line 38 is the $query one.
Parse error: parse error in /my_site/choicep.php on line 38
Reply With Quote
  #2 (permalink)  
Old 03-29-04, 10:20 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hello there,

Look closely - you are using double-quotations within double-quotations. You need to escape them if you do not want to close/open the string. In SQL, though, I've read somewhere that you should quote string values with single-quotations (double- would work, fine, I think).

The parse error should go away if you either escape those quotations around Y, but another thing I noticed is that the way you specified the year value is somewhat incorrect. What you wanted is perhaps this:

PHP Code:

$query "SELECT DATE_FORMAT(date, '%Y') AS 'year' FROM photos ORDER BY year"
I assumed that you have a field called "date" that holds date data type (i.e. yyyy-mm-dd). If that's the case, the above should get you what you wanted.

HTH.

BTW, more on date/time functions at MySQL.com: http://www.mysql.com/doc/en/Date_and...functions.html
__________________
Blavv =|
Reply With Quote
  #3 (permalink)  
Old 03-30-04, 02:01 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
I cant get anything to work; think I will have to try extracting the date when I upload to the database and create another colum.

All that happens now is cant complete query or I get the drop down list but there is no text in it.

I also tried substring(date, 0,4) As year but that had the same effect.

Anthony
Reply With Quote
  #4 (permalink)  
Old 03-30-04, 03:56 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Thinking while in the bath !!!!

Thinking to myself just now, that if the dropdown list of the form is changing length as I mess about with the script then it must be working.

The problem is that it is not displaying the data in the list which means a different problem !!!

Anthony
Reply With Quote
  #5 (permalink)  
Old 03-30-04, 10:01 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi again,

Okay, so the problemn is solved now?

If you're trying to do something new with SQL, it's always good to try out your SQL statement in a "pure" MySQL environment (i.e. command line MySQL client) to see if it's really working. One thing at a time, you know. If the query works, then onto PHP, but echoing every single step to see what you expect is what PHP is handling, and so forth. Debugging takes patience, but it's the necessary devil. Good luck!
__________________
Blavv =|
Reply With Quote
  #6 (permalink)  
Old 03-31-04, 07:44 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Hello

It is working in that there is a drop down list generated. I can also see the length of the list change as I try different things. If I select the arrow to expand the list there is no text on the list it is just white and if I run the mouse down the list the different "boxs" change to blue !!!!!

Anthony
Reply With Quote
  #7 (permalink)  
Old 03-31-04, 10:58 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
I am a bit depressed at the moment and am not thinking very well.

Here is the HTML output of the script :

<select name='date'>
<option value=''>
<option value=''>
<option value=''>
<option value=''>
</select>

As you can see the "value" is blank.

This is the current script that output the above.

PHP Code:

$query "SELECT DISTINCT DATE_FORMAT(date, '%Y') FROM photos ORDER BY date";

  
$result mysql_query($query)
       or die (
"Couldn't execute query."); 
Anthony
Reply With Quote
  #8 (permalink)  
Old 03-31-04, 05:01 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Daft

I said I was not very well - my excuse anyway.

Your method worked great blaw the problem was .............

I had not changed "date" to "year" in my form discription !!!!!!

Thanks again Anthony
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help regarding insertion of date time wajeeh_r ASP 1 03-04-04 04:05 PM
Problem with date in combobox. Periodically lose valu Danie Visual Basic 1 03-04-04 02:41 PM
Date From Access 2000 Database Kaikki ASP 4 09-25-03 06:04 PM
Header date Agent PHP 7 07-20-03 09:31 AM
selection of dup recs by latest date atworx Hot Scripts Forum Questions, Suggestions and Feedback 0 06-24-03 11:05 PM


All times are GMT -5. The time now is 10:21 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.