2011-07-25 133 views

回答

3

PHP的时间戳与Unix时间戳相同 - 自1970年1月1日以来的秒数。所以是的,一个简单的减法会给你一个秒钟的时间差,你可以通过潜水86,400(一天中的秒数) :

$days = (time() - $oldtimestamp)/86400; 
+0

我会添加'$ days = ceil($ days);'或'$ days = floor($ days);'。取决于要求 –

+0

ceil很好地工作!谢谢 – 3cross

0

尝试这种情况:

// Will return the number of days between the two dates passed in 
function count_days($a, $b) 
{ 
    // First we need to break these dates into their constituent parts: 
    $gd_a = getdate($a); 
    $gd_b = getdate($b); 
    // Now recreate these timestamps, based upon noon on each day 
    // The specific time doesn't matter but it must be the same each day 
    $a_new = mktime(12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year']); 
    $b_new = mktime(12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year']); 
    // Subtract these two numbers and divide by the number of seconds in a 
    // day. Round the result since crossing over a daylight savings time 
    // barrier will cause this time to be off by an hour or two. 
    return round(abs($a_new - $b_new)/86400); 
} 

Answer courtesy of the Doc

+0

只需获得时间戳差异,再加上可能与计算相关的“破坏”时间数据。 –

2

还有使用的,优选的,选择210和DateInterval类。

$now = new DateTime; 
$then = new DateTime; 
$then->setTimestamp($timestamp); 

$diff = $now->diff($then); 
echo $diff->days; 

以上也将提供的年数,月,日等应那些是你的兴趣(以及天总数如图所示)。