How to get aggregate days from PHP's DateTime::diff?

后端 未结 5 1665
傲寒
傲寒 2020-12-12 02:22
$now = new DateTime(\'now\');
$tomorrow = new DateTime(\'tomorrow\');
$next_year = new DateTime(\'+1 year\');

echo \"
\";
print_r($now->diff($tomorrow)         


        
5条回答
  •  悲&欢浪女
    2020-12-12 03:17

    its a bug http://bugs.php.net/bug.php?id=49778

    Thank you for your bug report.

    Days is indeed not set when creating a DateInterval using the constructor. A complication with this is that it is impossible to determine the number of days when months or years are specified, since these vary in length. It is possible to fill in the days field in some cases and leave it 0 in others. In any case, it should be documented that the days field is not always available.

提交回复
热议问题