Formatting a date range, with shortening if the interval falls entirely within a calendar month
This is a self-contained problem, with well-defined inputs and outputs.  To be maintainable, the code should reside in a function.

You have three distinct cases, and maybe more:


Start and end dates are the same day
Start and end dates fall in the same calendar month
Start and end dates span calendar months


Your solution should therefore be written to make it obvious that there are more than two cases.

Bugs in your implementation

intval($dateDiff-&gt;d) != 0 is an invalid check.  You would be treating the range from 2015-01-22 to 2015-03-22 as if it were one day.

Similarly, $d1-&gt;format('m') != $d2-&gt;format('m') is not quite right either.  It would be deceptive to format the range from 2014-12-26 to 2015-12-25 as 26. – 25.  December.  It might be acceptable to say 26. December – 25. December.  But really, it would be clearest to include the years as well.

Suggested solution

function formatDateRange($d1, $d2) {
    if ($d1-&gt;format('Y-m-d') === $d2-&gt;format('Y-m-d')) {
        # Same day
        return $d1-&gt;format('d. F');
    } elseif ($d1-&gt;format('Y-m') === $d2-&gt;format('Y-m')) {
        # Same calendar month
        return $d1-&gt;format('d.') . $d2-&gt;format(' – d. F');
    } elseif ($d1-&gt;format('Y') === $d2-&gt;format('Y')) {
        # Same calendar year
        return $d1-&gt;format('d. F') . $d2-&gt;format(' – d. F');
    } else {
        # General case (spans calendar years)
        return $d1-&gt;format('d. F Y') . $d2-&gt;format(' – d. F Y');
    }
}