I recently discovered a strange behavior of DateTime diff(). I just tried to calculate the difference between the following dates:
$first = new DateTime('2016-07-05T00:00:00');
$second = new DateTime('2016-12-30T23:59:59');
$interval = $first->diff($second);
echo $interval->h;
The strange thing is that this will output:
-1
That's a little bit confusing as I thought the public attributes of DateInterval are positive. Now if I try to create such a DateInterval by myself:
$interval = new DateInterval('P5M26DT-1H59M59S');
This will fail as negative values for hours are not allowed.
Is this behavior normal? If yes, how can i safely convert a DateInterval to string/from string (e.q. for storing in a DB)?