diarmadhi has the right idea; whatever's in your leasing agreement is the terms.
However: your landlord is probably right; they consider all notices to vacate from the prior month to be for the month following; like this: you say you gave 40-day notice, well, your leasing agreement probably states you must give at LEAST 30 days notice before vacating (this is standard). Some landlords might be nice and pro-rate you the extra rent but basically, if you gave your notice on the 20th of June, for example, your landlord would consider it to be notice that you would be living in the premises until the following first of the month, the first of August. This is unless you submitted your notice in written format stating that you would be leaving exactly 30 days later (on the 20th of July) and would expect to be compensated for the remaining rent; AND your landlord agreed to and signed these terms. Otherwise, he's right, and you won't get prorated, if the terms of the notice are assumed. As far as your security deposit goes, in CA the landlord has three weeks from the day you vacate to refund it, so you may not get that immediately either.
Things may be different in NY, but that's what I know about it from working in real estate here in CA

Sorry, and good luck.
__________________
Did I mention I have a sense of humor?
Right now it's an '03 Nissan Frontier Supercharged Desert Runner I call Starscream.
On order is a 1SS/ RS IBM with Black Rally Stripes, Beige Interior, manual transmission and a sunroof! Oh, and a stereo upgrade too! Ordered from CamaroScotty in WI. How will I get my baby home?