Step through it with the debugger, like we all do. If you don't know how, see this link So when month is 8, and day is 31, what do you think this will return:
elseif (month==4||month==6||month==8||month==10||month==12)
if day<=30
valid = true;
end
if day==31
valid =false;
end
Do you think it will return true or false for "valid"? All those months should have valid true if (day <= 31) && (day >= 1), not day<=30. You don't even need the next if.
elseif (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
if (day >= 1) && (day <= 31)
valid = true;
else
valid =false;
end
Also, I would not name variables year, month, or day since those are built-in functions.