I usually mess this up, but I am trying to get a year from a variable $date = 2011-01-01 I am trying to get 2011 from the variable…I tried this but it didnt echo anything out…
date(strtotime("Y"), strtotime($date));
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Or, if you don’t want to convert to date, and trust your input format:
The above requires PHP 5.4, on older versions you need two lines:
Or yet, this slightly faster alternative suggested by tigrang:
Or, an even faster way, as salathe suggested:
(everything after the first dash will be ignored, so
$yearwill contain an integer with the year part of the date (unlike the other options, where$yearwould be a string).