doucuo1642 2012-11-21 12:38
浏览 42

比较PHP中的unix时间戳[关闭]

In PHP I have:

$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));

How do I get the difference in seconds? I've tried the following:

$diff = abs(strtotime(date('m/d/Y h:i:s')) - strtotime($latest));
  • 写回答

2条回答 默认 最新

  • douzao2992 2012-11-21 12:42
    关注

    Use DateTime instead, it will make your code much cleaner.

    $latest = new DateTime($latest);
    $now = new DateTime();
    
    $diff = $latest->diff($now);
    echo $diff->format('%y years %m months %d days');
    
    评论

报告相同问题?