And the Prophet, may Allah bless him and grant him peace, forbade prayer at sunrise and sunset because the polytheists prostrate to the sun at those times, and Satan accompanies it. Although a Muslim praying does not intend to prostrate to the sun, the prohibition serves to block the means to polytheism, lest he imitate the polytheists in practices peculiar to them and thereby be led into polytheism. Hence, he forbade deliberately choosing these two times for prayer. This is Ibn ʿUmar’s wording in the two Sahīh collections. Deliberately setting out to pray at these times is therefore forbidden.
However, if an occasion arises for which prayer is prescribed—such as the prayer upon entering a mosque, the eclipse prayer, prostration upon recitation, the two units of prayer following circumambulation, or repeating a prayer with the local imam—there is a well-known disagreement among the scholars concerning these acts. The stronger view is that they are permissible and recommended, for they are good deeds involving no evil, and their opportunity is lost if they are left undone. The prohibition concerns deliberately setting out to pray at that time, because this resembles the disbelievers’ deliberate prostration at that time. When a prayer has no particular occasion, a person has chosen to perform it at that time, even if he did not choose the time for its own sake. By contrast, a prayer prompted by a particular occasion is performed because of that occasion; the time has no bearing on it whatsoever.
The Prophet, may Allah bless him and grant him peace, also issued a general prohibition against praying in graveyards, saying:
“The whole earth is a place of prayer, except graveyards and bathhouses.”
This was narrated by the compilers of the Sunan collections. It has been transmitted both with a connected chain to the Prophet and as a direct attribution to him by a Successor. The hadith masters have judged the connected version to be correct. Bathhouses are a refuge for devils, while prayer in graveyards was forbidden