the easiest way is to set a date equal to the first of the following month, and subtract one. That gives you the last day of the previous month, and then you can use the DAY() function.
Alternatively, have an array set to {31,28,31,30 etc), although you still get the issue with the leap day that way.