简体   繁体   中英

PHP strtotime date difference

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM