Inconsistencies with DataTime diff function in PHP

Viewed 45

While using the diff function in the DateTime class in PHP, I have been noticing some weird inconsistencies. All the following intervals should be less than a month since they are exactly 1 second less than a month, however, the last two are not:

date_default_timezone_set('Australia/Sydney');
$date_diff =  (new DateTime('2021-01-01 00:00:00'))->diff(new DateTime('2021-01-31 23:59:59'));
echo $date_diff->format('%y Year %m Month %d Day')."\n"; // 0 Year 0 Month 30 Day (expected)

$date_diff =  (new DateTime('2021-02-01 00:00:00'))->diff(new DateTime('2021-02-28 23:59:59'));
echo $date_diff->format('%y Year %m Month %d Day')."\n"; // 0 Year 0 Month 27 Day (expected)

$date_diff =  (new DateTime('2021-03-01 00:00:00'))->diff(new DateTime('2021-03-31 23:59:59'));
echo $date_diff->format('%y Year %m Month %d Day')."\n"; // 0 Year 1 Month 2 Day (???)

$date_diff =  (new DateTime('2021-10-01 00:00:00'))->diff(new DateTime('2021-10-31 23:59:59'));
echo $date_diff->format('%y Year %m Month %d Day')."\n"; // 0 Year 1 Month 0 Day (???)
0 Answers
Related