عَنْ أُبَيِّ بْنِ كَعْبٍ رَضِيَ اللَّهُ عَنْهُ أَنَّ رَسُولَ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَآلِهِ وَسَلَّمَ كَانَ يَعْتَكِفُ فِي الْعَشْرِ الْأَوَاخِرِ مِنْ رَمَضَانَ فَسَافَرَ سَنَةً فَلَمْ يَعْتَكِفْ، فَلَمَّا كَانَ الْعَامُ الْمُقْبِلُ اعْتَكَفَ عِشْرَيْنِ يَوْمًا
It is narrated from Sayyiduna Ubayy ibn Ka'b (may Allah be pleased with him) that the Messenger of Allah (peace and blessings be upon him) used to observe i'tikaf during the last ten days of Ramadan, but one year he had to undertake a journey, due to which he could not observe i'tikaf. Then, the following year, he observed i'tikaf for twenty days.