Here's a worked-out example that uses the full Lorentz transformation equations, not just the length-contraction and time-dilation equations, which are incomplete because they don't include relativity of simultaneity. I worked this out some time ago as a Usenet posting, with the equations in the crude pseudo-LaTex that people usually use on Usenet. I've converted the equations into real LaTex, and may have introduced some mistakes along the way, which I'll fix when I spot them.
------
The scenario: You stay behind on Earth while your twin goes on a round-trip space journey to Star Base Alpha, which is 4 light-years away. His ship can travel at a speed of 0.8c, so from your point of view he needs 5 years for the outbound trip and another 5 years for the return trip. The total trip duration is 10 years by your reckoning.
Relativity predicts that your twin experiences less elapsed time because of time dilation:
5 \sqrt {1 - {0.8}^2} = 3
years for each leg of the trip, and from his point of view the round trip lasts only 6 years.
The relativistic time dilation equation predicts that each twin's clocks "run slower" in the other twin's reference frame. So why can't your twin conclude that the trip must be shorter for you, than it is for him? The answer lies in the fact that your experiences are not symmetrical. Your twin is at rest in two different inertial reference frames, one during the outbound trip and another one during the inbound trip. You remain at rest in a single inertial reference frame during the entire journey. Your twin has to fire his spaceship's engines at the turnaround point. You do nothing.
To convince ourselves that both you and your twin do in fact agree that the elapsed time for the trip is longer on your clock than on his, we need to look carefully at how the two clocks behave in your twin's reference frames (note the plural). In order to do this properly, we need to use the full Lorentz transformation equations, and not just the length-contraction and time-dilation equations, which don't contain the "full story" of relativistic kinematics.
Let's call your reference frame S, in which an event has coordinates (x,t); let's call your twin's reference frame during the outbound trip S', in which an event has coordinates (x',t'); and finally, let's call your twin's reference frame during the return trip S", in which an event has coordinates (x",t").
To simplify the analysis, let's assume that your twin's ship can accelerate so rapidly that it can effectively change velocity instantaneously.
If S and S' are set up so that at the instant when your twin departs (moving in the +x direction with speed v), both you and he are at x = x' =0, and your clock and your twin's clock are synchronized so that they both read zero, we can use the usual textbook version of the Lorentz transformation equations
x^\prime = \gamma (x - v t)
t^\prime = \gamma (t - vx/c^2)
or inversely
x = \gamma (x^\prime + v t^\prime)
t = \gamma (t^\prime + v x^\prime / c^2)
where \gamma = 1 / \sqrt {1 - v^2 / c^2}. In our example, v = 0.8 light-year per year, and c = 1 light-year per year, so \gamma = 1.6667 = 5/3.
Your twin travels at a speed of 0.8c, so in your frame, S, he takes 5 years to cover the 4 light-years to Star Base Alpha, where he turns around (instantaneously). In frame S, his turnaround takes place at x = 4 light-years and t = 5 years.
We calculate the position and time of the turnaround in
his frame, S', as follows:
x^\prime = 1.6667 [4 - (0.8)(5)] = 0
which figures, because he's stationary in frame S', and
t^\prime = 1.6667 [5 - (0.8)(4)] = 3
So, during the twin's outbound trip (5 years according to you), 3 years elapse on his clock. That is, in your frame, S, his clock runs slower than yours, by a factor of 1 / \gamma = 0.6.
Now, how does
your clock behave in
his frame, S'?
First, let's calculate your position in S', at t' = 3 years. In S', you are moving "backwards" (-x' direction) at speed 0.8 light-years per year. So at t' = 3 years, you are located at x' = -(0.8)(3) = -2.4 light-years.
We can now calculate what your clock (which shows "S time") reads at this point in time in your twin's frame, S', using the inverse Lorentz transformation:
t = 1.6667 [3 + (0.8)(-2.4)] = 1.8
years. So, during your twin's outbound trip (3 years according to him), 1.8 years elapse on your clock, in his frame. That is, in his frame, S', your clock runs slower than his, by a factor of 1 / \gamma = 0.6.
Now, your twin fires his rocket engines to turn around very quickly so that he is now moving towards you with speed 0.8 light-years per year. He is now in a new reference frame, call it S".
We need a Lorentz transformation between S and S". We can't simply change primes to double primes in the equations that we used before, because S and S" do not coincide at x = x" = 0 and t = t" = 0. We have to use a more general version of the Lorentz transformation:
x^{\prime \prime} - x_0^{\prime \prime} = \gamma [(x - x_0) - v (t - t_0)]
t^{\prime \prime} - t_0^{\prime \prime} = \gamma [(t - t_0) - v (x - x_0) / c^2]
Inverse:
x - x_0 = \gamma [(x^{\prime \prime} - x_0^{\prime \prime}) + v (t^{\prime \prime} - t_0^{\prime \prime})]
t - t_0 = \gamma [(t^{\prime \prime} - t_0^{\prime \prime}) + v (x^{\prime \prime} - x_0^{\prime \prime}) / c^2)]
where some event (call it a "reference event") has coordinates x_0 and t_0 in frame S, and coordinates x_0^{\prime \prime} and t_0^{\prime \prime} in frame S". Note that if we set x_0 = x_0^{\prime \prime} = 0 and t_0 = t_0^{\prime \prime} = 0, we get back the original textbook version of the Lorentz transformation.
For this case, let the turnaround of your twin's ship be the reference event. Then x_0 = 4 light-years, t_0 = 5 years, x_0^{\prime \prime} = 0, t_0^{\prime \prime} = 3 years, and v = -0.8c (because now the ship is moving in the -x direction in S). Substituting these into the generalized Lorentz transformation gives
x^{\prime \prime} = 1.6667 [(x - 4) - (-0.8)(t - 5)]
t^{\prime \prime} - 3 = 1.6667 [(t - 5) - (-0.8)(x - 4)]
To get the reading on your clock in your twin's frame, just after the turnaound, we substitute x = 0 (you're at the origin of your own frame, S) and t" = 3 (the turnaround time in S"). This gives two equations in two unknowns, t (which is what we really want) and x" (which we get as a bonus):
x^{\prime \prime} = 1.6667 [-4 - (-0.8)(t - 5)]
0 = 1.6667 [(t - 5) - (-0.8)(-4)]
which we can solve to get x" = -2.4 and t = 8.2.
Therefore, in your twin's first reference frame, S', just before the turnaround, you are 2.4 light-years behind him and your clock reads 1.8 years; and in his second reference frame, just after the turnaround, you are 2.4 light years in front of him (remember, he's turned around!) and your clock reads 8.2 years. Apparently your position has shifted by 5.8 light-years and your clock has gained 6.4 years during the turnaround! But this has no physical significance as far as you yourself are concerned. These changes are simply because your twin is measuring space-time using a different coordinate system than before.
As an analogy, imagine an object resting on a sheet of paper that has a two-dimensional coordinate system (x,y) drawn on it. Rotate the the paper quickly that the object remains stationary because of inertia (like the old trick of yanking a tablecloth out from underneath the plates and silverware). The object's coordinates change suddenly, but nothing actually happens to the object itself.
The return trip is similar to the outgoing trip as far as elapsed time is concerned. In the classic words of textbook writers

, "the details are left as an exercise for the reader." 5 years elapse in your frame, S, and the total elapsed time on your clock, according to you, is 5 years (time at turnaround) + 5 years = 10 years. Meanwhile, in your twin's frame, S", 1.8 years elapse on your clock, so according to your twin, the total elapsed time on your clock is 1.8 years (during his outbound trip) + 6.4 years (the "jump" when he turns around) + 1.8 years (during his inbound trip) = 10 years.
The two of you agree that 10 years have elapsed on your clock. Likewise, you both agree that 6 years have elapsed on his clock. The fact that each of your clocks "runs slow" compared to the other one, during each leg of the trip, does not lead to an actual physical contradiction when the clocks are re-united at the end of the trip.
------
For another approach which gives the same result, see posting #3 in this thread:
https://www.physicsforums.com/showthread.php?t=69214