PHP strtotime date difference

前提是你 提交于 2019-12-25 03:39:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!