This is how you derive the equation for time dilation.
The metric used in special relativity is the Minkowski metric:
$$ ds^2 = -c^2dt^2 + dx^2 + dy^2 + dz^2 $$
and the basic principle of special relativity is the the line element $ds$ is an invariant, that is all observers in all inertial frames will measure it to have the same value.
Suppose we are using the coordinates $(t, x, y, z)$ and we observe an object moving at velocity $v$ in the $x$ direction (so $dy = dz = 0)$, then:
$$ ds^2 = -c^2dt^2 + dx^2 \tag{1} $$
But we expect the position of the object in our coordinates, $x$, to be given by:
$$ x = vt + x_0 $$
and therefore:
$$ dx = vdt $$
and if we substitute this in equation (1) we get:
$$ ds^2 = -c^2dt^2 + v^2dt^2 \tag{2} $$
Now shift to the frame of the moving object $(t', x', y', z')$. In these coordinates the objetc is stationary so $dx' = dy' = dz' = 0$ so:
$$ ds'^2 = -c^2dt'^2 \tag{3} $$
We started out by saying that all observers will agree on the value of the line element and that means $ds = ds'$, so equating equations (2) and (3) we get:
$$ c^2dt'^2 = c^2dt^2 - v^2dt^2 $$
And dividing both sides by $c^2$ and taking the square root:
$$\begin{align}
dt' &= dt \sqrt{1 - \frac{v^2}{c^2}} \\
&= \frac{dt}{\gamma} \tag{4}
\end{align}$$
And this is the basis of the time dilation. If we want to find the time $t'$ corresponding to a time $t$ then we simply integrate equation (4), and because $\gamma$ is a constant this integrates to:
$$ t' = \int_0^t \frac{dt}{\gamma} = \frac{t}{\gamma} $$
which is the equation that we all know and love.
This may seem like a long winded way of deriving the result, but note that this method is applicable to situations where the velocity is not constant. In that case the relationship between $dx$ and $dt$ is not linear, and the integral will be harder, however the working is exactly the same.