我想使用PHP DateTime的diff方法来获取两个日期之间的总天数,包括开始日期和结束日期。
我试过了:
$start_date = new DateTime('2014-01-01');
$end_date = new DateTime('2014-01-31');
$diff = $end_date->diff($start_date)->format("%a");
但是它返回30。
如何使用diff方法来包含结束日期?
试试这个
$start_date = new DateTime('2014-01-01');
$end_date = new DateTime('2014-01-31');
$end_date->modify('+1 day'); //add 1 to include the end date
$diff = $end_date->diff($start_date)->format("%a");
试试这个方法
$diff = $start_date ->diff($end_date );
echo $diff ->format('%y years %m months and %d days');