我想找到php中2个日期之间的区别。但是我没有得到确切的区别。 请帮我。 输出我必须得到“2年零0个月0天”。 但我得到的是“1年12个月4天”。 哪里错了?
$createddate="2015-12-24";
//find difference between the dates present-createddate of user
$now = time(); // present time
$your_date = strtotime($createddate);
$difference = abs($now - $your_date);
echo $difference;
// Years, months and days version
$years = floor($difference / (365*60*60*24));
//echo $years;
$months = floor(($difference - $years * 365*60*60*24) / (30*60*60*24));
//echo $months;
$days = floor(($difference - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
//echo $days;
$membersince .= $years.' years '.$months.' months and '.$days.' days';
答案 0 :(得分:4)
<?php
$datetime1 = new DateTime();
$datetime2 = new DateTime('2015-12-24');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years, %m months, %d days');
echo $elapsed;