html - PHP list all years between two dates -


this question has answer here:

i created form user needs fill out. based on age answer, need list years between current year , birth year. able figure out how convert age number actual year with:

function birthday_year($years) {     return date('y', strtotime($years . ' years ago')); }  $byear = birthday_year($age); echo $byear; 

i'm having difficult time listing years between birth year , current year. guidance appreciated. able find other code examples list months when try manipulate code years doesn't work @ all. 3rd week php, still pretty new me. thanks!

there several ways handle it, rather simple answer loop year you've decided on, current year.

for ($nyear = $byear; $nyear <= date('y'); $nyear++) {         echo $nyear . "\n"; } 

reverse order :

for ($nyear = date('y'); $nyear >= $byear; $nyear--) {         echo $nyear . "\n"; } 

note code wrong assume n years ago birth year. if 9 today (and birthday tomorrow) code (today) born in 2006 , tomorrow 2005.


Comments