Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hoe to extract the year part of a date field.

Status
Not open for further replies.

AnNguyen

Programmer
Joined
Jul 18, 2002
Messages
33
Location
US
Hello all

How can i extract the year part of a given date field? Let say the users have a form with a date field and a submit button, they enter '20-jun-2004' or '2004/06/20' and hit submit. Is there any built-in PHP function to get the year of the date field or i have to write my own code to do this.
The date(Y) function give me the year of the local time and it would be nice to have something like date('20-jun-2002',Y) to give 2002.

Hope that someone would help me out.

 
Code:
date('Y', strtotime($input));

Would be the built in function to accomplish what you want, but it is not a perfect solution, and there are several formats which it will choke on.
 
Thank you skiflyer much appreciated.
 
Speaking from experience:
IMO it is best to split your date input into three components. That is the 100% safe way to get valid parts. Also, then there's nothing to extract and users don't have a chance to provide disparate formats.
 
I second that DRJ's advice for all web-basd applications.

If you're talking about an application with high rates of data entry, make sure you cause the cursor to automatically overflow from field to field, or you will get angry users.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top