Fetching only year from mysql timestamp 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
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Fetching only year from mysql timestamp date

Post by apek »

how to fetch only year from mysql database??and the data type is timestamp???

Code: Select all

Select * from repairbreakdown WHERE noTag = '{$_POST["noTag"]}' and year(waktuTamat) = '{$_POST["year"]}'"
is my code correct????
i use year(waktuTamat)...is this true?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you only want to fetch the year then you can use YEAR() to specify it in the SELECT statement:

Code: Select all

SELECT field1, field2, field3, YEAR(date_field) AS date_field_year FROM table
It is good practise to specify which fields you wish to return from the table. It is also a good idea to not put user inputted data directly into a query without validating it first.

If you are unsure whether your code is correct does that mean that it is not doing what you expect?

Mac
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

but can i use that to fetch year from timestamp data type???
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

hello????
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you tried it?

Mac
Post Reply