对月份和年份的日期时间差异感到困惑

时间:2016-01-13 17:08:08

标签: php function datetime

我已经设定了计算两个日期之间的年龄的函数,但我对月份和年份有问题,因为月份可能是29天,30天或31天。

我很困惑,这会导致任何问题吗?如果是,那么有人可以提出解决方案。

这是我的功能

function time_age($date_time1,$date_time2 = ''){
    //store current date-time if it hasn't been given
    if (empty($date_time2)){
        $date_time2 = date('Y-m-d H:i:s');
    }
    //get date-time difference in seconds
    $time_age = strtotime($date_time2) - strtotime($date_time1);
    //to store if seconds, minutes, hours, days, weeks, months, years
    $time_age_type = 'seconds';
    if ($time_age >= 60 and $time_age < 3600){
        $time_age_type = 'minutes';
        $time_age = number_format($time_age / 60, 0);
    }elseif ($time_age >= 3600 and $time_age < 86400){
        $time_age_type = 'hours';
        $time_age = number_format($time_age / 3600, 0);
    }elseif ($time_age >= 86400 and $time_age < 604800){
        $time_age_type = 'days';
        $time_age = number_format($time_age / 86400, 0);
    }elseif ($time_age >= 604800 and $time_age < 2629743){
        $time_age_type = 'weeks';
        $time_age = number_format($time_age / 604800, 0);
    }elseif ($time_age >= 2629743 and $time_age < 31556926){
        $time_age_type = 'months';
        $time_age = number_format($time_age / 2629743, 0);
    }elseif ($time_age >= 31556926){
        $time_age_type = 'years';
        $time_age = number_format($time_age / 31556926, 0);
    }
    return $time_age.' '.$time_age_type.' ago';
}

2 个答案:

答案 0 :(得分:2)

我会建议这个替代解决方案,所以我从https://php.net

中选择了这个例子
<?php
$val1 = '2014-03-18 10:34:09.939';
$val2 = 'now';

$datetime1 = new DateTime($val1);
$datetime2 = new DateTime($val2);
echo "<pre>";
var_dump($datetime1->diff($datetime2));

https://secure.php.net/manual/en/datetime.diff.php#114656

答案 1 :(得分:1)

我使用的这个功能怎么样?

/**
 * Get a string of length of time elapsed since a specified time.
 *
 * @param int $time the specified time to check
 * @return string returns a string stating the length of time ago.
 */
function timeSince($time)
{
    $timeDifference = time() - $time;

    $timeTokens = [
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'min',
        1 => 'sec'
    ];

    foreach ($timeTokens as $unit => $text) {
        if ($timeDifference < $unit) continue;

        $noUnits = floor($timeDifference / $unit);

        return $noUnits . " " . $text . (($noUnits > 1) ? "s ago" : " ago");
    }
}

<强>用法

echo timeSince(time() - 86400);

<强>输出

1 day ago

您还可以使用DateTime课程&#39; diff功能:

OO 方法:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);

echo $interval->format('%R%a days');

程序方法:

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);

echo $interval->format('%R%a days');

注意:如果您正在使用DateTime类并希望找到两个 UNIX 时间戳之间的差异,那么您必须在{I}之前添加@符号时间戳,例如:

new DateTime("@" . $yourUnixTimestamp);

否则会引发错误。