The graphic guy wants to display dates like
16. February
17. - 24. February
, or25. February - 02. March
.
Here is my solution:
$d1 = new \DateTime('2015-02-06');
$d2 = new \DateTime('2015-03-02');
$dateDiff = $d1->diff($d2);
if (intval($dateDiff->d) != 0) {
$d = $d1->format('d.') . ' ' . ($d1->format('m') != $d2->format('m') ? $d1->format('F') : '') . ' - ' . $d2->format('d. F');
} else {
$d = $d1->format('d. F');
}
I'm wondering if the this comparison:
$d1->format('m') != $d2->format('m')
is the best way to check whether $d1
and $d2
are in the same or different months.
($dateDiff = $d1->diff($d2)
tells me that the two dates are 24 days apart, but it doesn't tell me if they are in different calendar months.)