Know that what has passed you by was not going to befall you; and that what has befallen you was not going to pass you by

On the authority of Abdullah bin Abbas (May Allah be pleased with him), who said :

One day I was behind the prophet and he said to me:

“Young man, I shall teach you some words [of advice]: Be mindful of Allah, and Allah will protect you.

Be mindful of Allah, and you will find Him in front of you.

If you ask, ask of Allah; if you seek help, seek help of Allah.

Know that if the Nation were to gather together to benefit you with anything, it would benefit you only with something that Allah had already prescribed for you, and that if they gather together to harm you with anything, they would harm you only with something Allah had already prescribed for you. The pens have been lifted and the pages have dried.”

– Narrated by Tirmithi, who said it is true and fine hadith

In a version other than that of Tirmithi it reads

Be mindful of Allah, you will find Him before you.

Get to know Allah in prosperity and He will know you in adversity.

Know that what has passed you by was not going to befall you; and that what has befallen you was not going to pass you by.

And know that victory comes with patience, relief with affliction, and ease with hardship