比较 PHP 中的 unix 时间戳

use*_*964 4 html php mysql

在 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)

xda*_*azz 5

使用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)