在 PHP 中我有:
$diff = abs(strtotime(date('m/d/Y h:i:s')) - strtotime($latest));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
echo floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
Run Code Online (Sandbox Code Playgroud)
如何获得以秒为单位的差异?我尝试过以下方法:
$diff = abs(strtotime(date('m/d/Y h:i:s')) - strtotime($latest));
Run Code Online (Sandbox Code Playgroud)
使用DateTime它,它会让你的代码更加简洁。
$latest = new DateTime($latest);
$now = new DateTime();
$diff = $latest->diff($now);
echo $diff->format('%y years %m months %d days');
Run Code Online (Sandbox Code Playgroud)