Reputation: 331
I want to get the age of users. I tried:
$dStart = strtotime('1985-10-24');
$dEnd = strtotime('2014-07-30');
$dDiff = $dEnd - $dStart;
echo date('Y',$dDiff);
But get 1998 instead 28, what would be the right code?
Upvotes: 0
Views: 8514
Reputation: 219824
strtotime()
is not made for date math. DateTime()
is much better suited for this:
$dStart = new DateTime('1985-10-24');
$dEnd = new DateTime('2014-07-30');
$dDiff = $dStart->diff($dEnd);
echo $dDiff->y;
Upvotes: 4