Page 1 of 1

display only year from a full format date...

Posted: Wed Jun 23, 2004 11:47 pm
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~

Posted: Wed Jun 23, 2004 11:53 pm
by Illusionist

Code: Select all

substr($row['date'],0,4);

Posted: Wed Jun 23, 2004 11:54 pm
by Weirdan

Code: Select all

select year(yourdatecolumn) as `year` from yourtable....

Posted: Wed Jun 23, 2004 11:56 pm
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...

Posted: Wed Jun 23, 2004 11:56 pm
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

Posted: Wed Jun 23, 2004 11:58 pm
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

Posted: Thu Jun 24, 2004 12:16 am
by PrObLeM
another way!!!

Code: Select all

$array = explode('-', '2004-06-30');
echo $array[0];

Posted: Thu Jun 24, 2004 12:25 am
by kevin7
wow... so many choices~ tq ya all... :D