Actually you can just remove that 2nd instance of $datecut. They will both have the same result, so there's no point in having it there twice. I thought I had already taken that out, but I guess I forgot about it.
Thanks for pointing out the errors in the code. I guess I was more tired than I thought when I did that. I updated the file and the post above with the correct code though.