Praise be to Allah.
If someone falls asleep before sunset and does not wake up until after the break of dawn on the following day, his fast of that day is not valid according to the majority of scholars, because he did not form the intention to fast during the night before, and the night begins with the setting of the sun.
The majority of scholars stipulate that one should form the intention to fast the night before, for each day of Ramadan, and it is not sufficient to form one intention at the beginning of the month, because the Prophet (blessings and peace of Allah be upon him) said: “Whoever did not form the intention to fast before dawn, there is no fast for him.” Narrated by Abu Dawood (2454), at-Tirmidhi (730) and an-Nasaa’i (2331). According to a version narrated by an-Nasaa’i: “Whoever did not form the intention at night to fast before dawn, there is no fast for him.” This hadith was classed as saheeh by al-Albaani in Saheeh Abi Dawood.
The matter of the intention is easy: if it crosses a person’s mind sometime between sunset and dawn that he is going to fast the next day, then this is the intention. If he eats or drinks knowing that this is for the purpose of fasting the following day, then he has intended to fast.
But the one who falls asleep before sunset does not do any of these things.
The view of the Maalikis, and of Ahmad according to one report, is that one intention is sufficient at the beginning of the month.
Based on this view, the fast of this person who fell asleep is valid.
Ibn Qudaamah (may Allah have mercy on him) said: The intention is required for every day. This is the view of Abu Haneefah, ash-Shaafa‘i and Ibn al-Mundhir.
It was narrated from Ahmad that one intention is sufficient for the entire month, if he intends to fast the entire month. This is the view of Maalik and Ishaaq. That is because he formed the intention at a time the nature of which makes it valid to form an intention to fast, so it is valid, just as if he formed the intention to fast each day the night before.
In our view, it is an obligatory fast, so it is required to form the intention for each day the night before, as is the case when making up a missed fast.
Moreover, each single day is an act of worship, and if one of the days is rendered invalid, it does not invalidate any other day; doing something contrary to fasting on one day does not spoil any other days. Thus it is like fasting to make up a missed fast. Therefore each day is separate from the first day (so one intention formed at the beginning of the month is not sufficient).
End quote from al-Mughni (3/23).
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) thought that the view of the Maalikis is more likely to be correct.
He (may Allah have mercy on him) said: He said: fasting each day is obligatory, so one must form the intention for each day at that time. For example, in Ramadan one needs to form thirty intentions.
Based on that, if a man falls asleep after ‘Asr in Ramadan, and does not wake up until after the break of dawn on the following day, his fast of that day is not valid, because he did not form the intention to fast that day from the night before. What the author mentions is the well-known view of the madhhab.
They gave as the reason for that the fact that each day is a separate act of worship, therefore if the fast of Sunday is rendered invalid, it does not invalidate the fast of Monday, for example.
Some of the scholars were of the view that in cases where it is stipulated that an act of worship be done consecutively, it is sufficient to form the intention at the beginning, unless the sequence is interrupted for a valid reason, in which case the intention should be renewed [when resuming that act of worship].
Based on that, if a person intended on the first day of Ramadan to fast the entire month, that intention is sufficient for the entire month, so long as nothing happens to make him interrupt it, such as if he travels during Ramadan; in that case, when he resumes fasting, he must renew his intention.
This is the more correct view, because if you ask any Muslim about that, he will tell you that he intended to fast from the beginning of the month until the end.
Based on that, if the intention was not formed every night in a true sense, then it is still deemed to have taken place, because the basic principle is that the intention does not expire. Therefore we say: if an interruption to the fast occurs for a valid reason, then the person resumes fasting, he must renew his intention.
This view is what one feels comfortable with, and it is not possible for people to adhere to any other view.
End quote from ash-Sharh al-Mumti‘ (6/356).
To be on the safe side, you should follow the majority view, refrain from eating and drinking for the rest of the day, then make up the day.
It should be noted that the difference of scholarly opinion has to do with one who falls asleep before sunset and carries on sleeping until the break of dawn. But if he wakes up during the night, even for a moment, and it occurs to him that he will be fasting the following day, then his fast is valid, and there is no difference of scholarly opinion concerning that.
And Allah knows best.