Michel G. Posted March 22, 2013 Share Posted March 22, 2013 I would like to retreive the year from a date I enter (and which is not necessarily the date of the day!). For instance, I enter something like "$date = '15-09-2010';" How can I retrieve "2010" from this declaration? Link to comment Share on other sites More sharing options...
margaux Posted March 22, 2013 Share Posted March 22, 2013 There are a number of approaches you can take. $date is a string - so you could use substr $date = '15-09-2010'; $year = substr($date,-4); or since the date is formatted with dashes - you could use explode $date = '15-09-2010'; $date_parts = explode('-', $date); $year = $date_parts[2]; 2 Link to comment Share on other sites More sharing options...
HartleySan Posted March 22, 2013 Share Posted March 22, 2013 This might answer your question: http://stackoverflow.com/questions/2222851/convert-string-to-date-in-php Link to comment Share on other sites More sharing options...
Michel G. Posted March 23, 2013 Author Share Posted March 23, 2013 Thanks a lot! At this time, the shortest one for my purpose is the following: $year = substr($date,-4); In the future, I'll further consider alternatives. Link to comment Share on other sites More sharing options...
Antonio Conte Posted March 25, 2013 Share Posted March 25, 2013 I would use DateTime::format("y"); Link to comment Share on other sites More sharing options...
Recommended Posts