zu and für are quite tricky. A lot of times when you would think "for" fits it would be "zu". For example:
For example : zum Beispiel 😀
For the birthday : zum Gebursttag
For sale : zu verkaufen (pay attention that here a verb is used after zu, that's why verkaufen is with a small V)
In your example -
Why zu is correct, and not für is in my opinion depends on the object, if the pills are intended for the night or not.
In this case the night won't receive the pills. They are intended for you.
so gramatically I think für die Nacht is possible, but I would use zu.
Regarding the 2nd example -
Für works here, again because of the explanation above