php,多个日期之间的区别

时间:2014-06-18 08:27:52

标签: php date difference

我有多个日期,例如:
1395637200个
1402894800个
1403499600个

我想对他们做的是检查他们中的哪一个是:
一个。 “上一个”日期
湾“下一个”日期
我怎样才能在PHP中使用它们(±100-500)?

3 个答案:

答案 0 :(得分:0)

以下是以分钟为单位的时差示例。

$to_time = strtotime("2008-12-13 10:42:00");
$from_time = strtotime("2008-12-13 10:21:00");
echo round(abs($to_time - $from_time) / 60,2). " minute";

通过这种方式,您可以通过再次除以60来找到小时差异

然后,如果你想找到天数的差异,那么除以60 * 60 * 24

答案 1 :(得分:0)

这是你想要的吗?肯定有一种正确的方法可以做到这一点。

$dates = array("1395637200","1402894800","1403499600");
$previousDate = "";
$nextDate = "";
$now = time();

foreach($dates as $date)
{
    // Older
    if($date < $now) {
        $previousDate = $date;
    }
    // Next
    elseif($date > $now)
    {
        $nextDate = $date;
    }
}

echo "Previous date is $previousDate <br> Next date is $nextDate ";

答案 2 :(得分:0)

试试这个:

// Current array of dates
$dates = array(1395637200, 1402894800, 1403499600);

// Add current date to array and store in $cur
$dates[] = $cur = time();

// Sort dates (with current date added)
sort($dates);

// Get key of current date
$today = array_search($cur, $dates);

// If current date is first in array, there is no previous date.
// Otherwise, get date for previous key
$prev = $today == 0 ? NULL : $dates[$today-1];

// If current date is last in array, there is no next date.
// Otherwise get date for next key
$next = $today == count($dates)-1 ? NULL : $dates[$today+1];

See demo