Did Jesus visit the temple before the wise men came? I was looking into the accounts of Jesus' birth both in Matthew and Luke and tried to make something of a chronology of the events on a piece of paper so that I can get it clear in my head.

The thing that I came to notice is that there could be a large time gap in between Luke 2:38 and Luke 2:39...

Now if we read the 2 accounts carefully we will understand that the wise men came to Jerusalem (The city of the King), expecting to find the newborn King there. However, Herod, consulting the scholars of the day sent them to Bethlehem (as it has been prophesied) (Mat. 2:1-6). 

Now, we are not actually told that they actually went to Bethlehem as the star appeared and guided them again. However, certainly that is the assumption of most people.

Later we read (Mat. 2:16-18) that Herod went about killing all the male children aged 2 years or less, based on the information he had acquired from the wise men, concerning the time that the star appeared (Mat. 2:7).

So it is safe to say that the wise men came to Jesus anytime before He was 2 years of age.

However, what makes it more interesting is that it is written that after the wise men left, Joseph was told in a dream to flee to Egypt with Mary and Jesus.

Knowing this, we look back into the account by Luke and see that they went into the Temple in Jerusalem, for the cleansing of Mary (Luke 2:22) as it is written in the Law (Lev. 12:3-8)... According to this passage for a male child this is done 33 days after (birth?). 

Which would mean that they visited the temple before the wise men came to them?

And then returned to Bethlehem where the wise men came (even though Lk. 2:39 says they went back to Nazareth - assuming there is a gap and this speaks after their return from Egypt.) OR they went back to Nazareth straight after the cleansing in the temple (approx. a little over a month after the birth), meaning that even though the wise men were sent to Bethlehem by Herod, the star guided them to Nazareth..? 

So, my question:

Now, more than one question arise from the comments above, however my main question is:

According to my observations, is it safe to say that Jesus went to Jerusalem and into the Temple before the wise men got there? Are there other places in Scripture that confirm this or is there perhaps a flaw in my logic?
Here is my attempt to harmonize the accounts of Matthew 2 and Luke 2, This assumes a literal retelling of the story. (Luke as a careful writer has been vindicated again and again by archaeology.)


  NAZARETH (above Samaria to the north) to BETHLEHEM


Luke 2:4 Joseph also went up from Galilee, out of the city of Nazareth, into Judea, to the city of David, which is called Bethlehem, because he was of the house and lineage of David,


  BETHLEHEM (just south of Jerusalem)


Luke 2:7 Jesus born in a manger

Luke 2:8-20 Jesus still in manger when shepherds see them

Luke 2:21 Eight days for circumcision


  JERUSALEM


Luke 2:22-24 Now when the days of her purification [33 days] according to the law of Moses were completed, they brought Him to Jerusalem to present Him to the Lord. [They gave gifts as poor people would, so the Magi had not given them riches at this time.]

Luke 2:25-35 Simeon sees Jesus.

Luke 2:36-38 Anna sees Jesus.


  JERUSALEM to NAZARETH


Luke 2:39 So when they [Joseph and Mary] had performed all things according to the law of the Lord, they returned to Galilee, to their own city, Nazareth.


  Approx. 1-year GAP. Then NAZARETH to JERUSALEM to HILL COUNTRY(?)


Luke 2:41 We learn from this passage that they went to JERUSALEM every year for the Passover. So we see them no longer in Nazareth, but in Jerusalem. Perhaps they turned aside to Bethlehem at that point to visit relatives and family. In Luke 1:39, Mary visits Elizabeth in the "hill country of Judah," which, according to this map, includes Bethlehem. After all, this is Joseph and Mary’s home town where their families could still be living, so it’s not inconceivable that they would go see them or stay with them during the Passover. Bethlehem is just below Jerusalem -- see the map above. 

Matthew 2:1-8 “Now after Jesus was born in Bethlehem of Judea in the days of Herod the king, behold, wise men from the East came to Jerusalem,” [The Magi visit Jerusalem and Herod, where they are told by the scribes that the ruler is to come out of Bethlehem.] 

Matthew 2:9-11 “When they [Magi] heard the king, they departed; and behold, the star which they had seen in the East went before them, till it came and stood over where the young Child was.” [This was not their home in Nazareth, but “the house” (Matt. 2:11), perhaps a family’s or relative’s home. This Magi’s visit was perhaps the first Passover after Luke’s account, for Herod said to kill children 2 years old and under.]

Matthew 2:12 The Magi return home without telling Herod.


  BETHLEHEM to EGYPT


Matthew 2:13-15 “Now when they [Magi] had departed, behold, an angel of the Lord appeared to Joseph in a dream, saying, “Arise, take the young Child and His mother, flee to Egypt, and stay there until I bring you word; for Herod will seek the young Child to destroy Him.”

Matthew 2:16 babies killed in Bethlehem. [The slaughter may not have happened until several months after the Magi and family had left, when Herod finally wises up and causes those children 2 years old and under to be killed. It took time for the Magi to journey here from the east, so maybe they told Herod that the Ruler was born when they first saw the star? Adding that time and the time the Magi departed would come to about 2 years by Herod’s reckoning. If this is so, then the Magi may have come here at the first Passover after Luke’s account.]

Matthew 2:19 Herod dies. [The family survived in Egypt on the gifts of the Magi.]


  EGYPT to NAZARETH


Matthew 2:22-23 Joseph is told to return, for Herod was dead. “…. And being warned by God in a dream, he turned aside into the region of Galilee. And he came and dwelt in a city called Nazareth, ….” Now we are back to Nazareth.