Sayyiduna Samurah (may Allah be pleased with him) reports that the Prophet (peace and blessings be upon him) said: "Do not pray when the sun rises, nor when it sets, for it rises between the two horns of Shaytan and sets between the two horns of Shaytan."
Ahmad, al-Bazzar and al-Tabarani in al-Mu'jam al-Kabir recorded it. It is reported through various routes, some of which are similar to this, and some of which have these words: "The Prophet (peace and blessings be upon him) used to instruct us that we could pray at any hour of the night or day, except that he instructed us to avoid praying at the time the sun rises and at the time it sets. He used to say: 'When it sets, Shaytan sets along with it, and when it rises, Shaytan rises along with it.'" Ahmad's narrators are thiqat (trustworthy).