自[44]年前回归以来的日子过去了#39;

时间:2014-04-16 23:07:47

标签: php

我试图找到一个解决方案,但没有运气。我有格式2014-04-03 19:21:30的$ date 我试图回应那天使用函数已经过了多少天:

function timePassed($time){
        $time = time() - $time;
        $tokens = array (
            31536000 => 'year',
            2592000 => 'month',
            604800 => 'week',
            86400 => 'day',
            3600 => 'hour',
            60 => 'minute',
            1 => 'second'
        );

        foreach ($tokens as $unit => $text) {
            if ($time < $unit) continue;
            $numberOfUnits = floor($time / $unit);
            return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
        }
    }

我44年前...我做错了什么?

1 个答案:

答案 0 :(得分:2)

这将为您提供使用逻辑所用的时间并返回1 week 5 days 21 hours 4 minutes 36 seconds

使用strtotime(),我将时间转换为相对于1/1/1970的数字。您现在可以与time()进行比较。我还减去了每个步骤中经过的时间量,因此它可以继续找到较小的增量。

此代码将生成:

1 week
1 year
20 minutes 38 seconds 

<?php
echo timePassed("2014-04-03 19:21:30") . "<br>";
echo timePassed("2013-02-03 19:21:30") . "<br>";
echo timePassed("2014-04-16 16:20:00") . "<br>";

function timePassed($time){
    $time = time() - strtotime($time);

    $tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );

    $return = "";
    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        $time -= ($numberOfUnits * $unit);
        $return .= $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'') . " ";
        if ($unit > 60){
            // return if match greater than minutes
            return $return;
        }
    }
    return $return;
}

?>

要跟进您的上一个请求,以下代码应该足以根据您的需要对其进行修改。此代码将生成:

1 week
2013-02-03 19:21:30
23 hours 

<?php
echo timePassed("2014-04-03 19:21:30") . "<br>";
echo timePassed("2013-02-03 19:21:30") . "<br>";
echo timePassed("2014-04-16 16:20:00") . "<br>";


function timePassed($time){
    $origtime = $time;
    $time = time() - strtotime($time);
    $tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );
    $return = "";
    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        $time -= ($numberOfUnits * $unit);
        $return .= $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'') . " ";
        if ($unit > 60){
            // return if match greater than hours
            if ($unit > 2592000 ) {
                // if units greater than one month, show the original time
                return $origtime;
            }
            elseif ($unit == 2592000  && $numberOfUnits > 1){
                // if units is months, show the original time if more than one month
                return $origtime;
            }
            else {
                // units greater than minutes, show the time without further detail
                return $return;
            }
        }
    }
    return $return;
}

?>