That's a very poor problem statement.
In what way did it fail?
The test code I posted was not meant to pasted directly into your script and then work as expected. How did you take the code I showed and incorporate it into your script?
The key part is the assignment of $numberDays. The assignment code I gave as well Sunfighter's will give the correct results. The assignment code that felgall added one more day than was needed/wanted.