Reputation: 15
I want to find difference between 2 dates in php.But I am not getting exact difference. please help me. The output I must get as "2 years 0 months 0 days". But I am getting as "1 years 12 months and 4 days". where I am wrong?
$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';
Upvotes: 0
Views: 484
Reputation: 219824
<?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;
Upvotes: 4