[SOLVED] display only year from a full format date...

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
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

display only year from a full format date...

Post by kevin7 »

i've a date column stored in a table... the format is 2004-06-30 (Y-m-d)
...

now, i want to retrieve it from the database and display only the year...
for example... if the date stored is 2004-06-30, then it should return 2004...

what query should i use? i've try to_date... they came out error...

how can i do that?

tq~
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

Code: Select all

substr($row['date'],0,4);
Last edited by Illusionist on Wed Jun 23, 2004 11:56 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

select year(yourdatecolumn) as `year` from yourtable....
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Post by kevin7 »

Fatal error: Call to undefined function: sub_str() in c:\inetpub\wwwroot\mc\employee_payslip.php on line 135

... hmm.. the function was invalid...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Or you could do it in the query part, eg:
SELECT EXTRACT(YEAR FROM '1999-07-02'); would get '1999' more examples here
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

kevin7 wrote:Fatal error: Call to undefined function: sub_str() in c:\inetpub\wwwroot\mc\employee_payslip.php on line 135

... hmm.. the function was invalid...
I accidentally added an _ in there
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

another way!!!

Code: Select all

$array = explode('-', '2004-06-30');
echo $array[0];
kevin7
Forum Commoner
Posts: 96
Joined: Fri May 21, 2004 6:54 am

Post by kevin7 »

wow... so many choices~ tq ya all... :D
Post Reply