SkyRanger Posted March 2, 2012 Share Posted March 2, 2012 Hello, I seem to be having a problem. I am trying to extract the year from a date 2012-03-01 echo "2012"; I have tried this and it only displays 1969 $dateorig = "2012-03-01"; $new_year = date("Y", strtotime($dateorig)); echo $new_year; Quote Link to comment https://forums.phpfreaks.com/topic/258089-extract-year/ Share on other sites More sharing options...
Pikachu2000 Posted March 2, 2012 Share Posted March 2, 2012 That particular format isn't supported by strtotime(), but since you already have the date in a string, and php's date() function is rather slow, why not just explode it? $date = '2012-06-03'; $year = explode('-', $date); echo $year[0]; Quote Link to comment https://forums.phpfreaks.com/topic/258089-extract-year/#findComment-1323004 Share on other sites More sharing options...
SkyRanger Posted March 2, 2012 Author Share Posted March 2, 2012 It worked. Thanks Pikachu, greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/258089-extract-year/#findComment-1323007 Share on other sites More sharing options...
PFMaBiSmAd Posted March 3, 2012 Share Posted March 3, 2012 Actually that format is supported by strtotime. If your original code didn't work, that's not your actual code or actual data. I'll guess the date was from someplace like a form or a database and it wasn't what you thought (probably an empty value.) Quote Link to comment https://forums.phpfreaks.com/topic/258089-extract-year/#findComment-1323478 Share on other sites More sharing options...
Pikachu2000 Posted March 3, 2012 Share Posted March 3, 2012 It is? I saw it in the list with slashes, but not hyphens. I must have overlooked it. Quote Link to comment https://forums.phpfreaks.com/topic/258089-extract-year/#findComment-1323482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.