问题
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?
回答1:
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;
Demo
来源:https://stackoverflow.com/questions/25026602/php-strtotime-date-difference