我想在php中考虑年,月,日来计算日期差异。现在程序计算考虑所有日期时间对象参数的日期差异。
$DTObj = new DateTime();
$pdate = DateTime::createFromFormat("ymd", 141118);
$diff = $DTObj->diff($pdate);
$dday = $diff->format("%a");
我怎样才能实现这一目标。
答案 0 :(得分:1)
阅读并试试这个:
# create DateTime object with time at 00:00:00
$from = new DateTime('today');
# create DateTime object from ymd format
# format character ! resets all fields (year, month, ...) to the Unix Epoch and then sets the inputed date
$to = DateTime::createFromFormat('!ymd', '141118');
# calculate difference between $from and $to and return DateInterval object
$diff = $from->diff($to);
# echo difference of total number of days
echo $diff->format('%a');