Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自返回后的天数';44年前';_Php - Fatal编程技术网

Php 自返回后的天数';44年前';

Php 自返回后的天数';44年前';,php,Php,我试图找到一个解决办法,但没有成功。我有一个$date,格式为2014-04-03 19:21:30 我尝试使用该函数回显自那天以来已经过去了多少天: function timePassed($time){ $time = time() - $time; $tokens = array ( 31536000 => 'year', 2592000 => 'month', 604800

我试图找到一个解决办法,但没有成功。我有一个$date,格式为2014-04-03 19:21:30 我尝试使用该函数回显自那天以来已经过去了多少天:

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':'');
        }
    }
function timePassed($time){
$time=time()-$time;
$tokens=数组(
31536000=>“年”,
2592000=>“月”,
604800=>“周”,
86400=>“天”,
3600=>“小时”,
60=>“分钟”,
1=>“秒”
);
foreach($unit=>$text形式的令牌){
如果($时间<$单位)继续;
$numberOfUnits=楼层($time/$unit);
返回$numberOfUnits.'.$text.($numberOfUnits>1)?'s':'';
}
}

我是44年前得到的。。我做错了什么?

这将使用您的逻辑为您提供经过的时间,并返回
1周5天21小时4分钟36秒

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

该代码将生成:

1 week
1 year
20 minutes 38 seconds 
1 week
2013-02-03 19:21:30
23 hours 
"



44年前是Unix时代,
$time=0
。我有一个类似的问题。你可以在那里得到答案:@Ibu谢谢你!您是如何将日期
2014-04-03 19:21:30
传递到timePassed函数的?您还应该查看一下,并指出OP的错误,有没有一种方法可以整理回显:如果刚刚发布:几秒钟前回显如果几分钟前回显:echo Xm Xs ago如果几小时前回显:echo X小时前如果几天前回显:echo X天前如果几周:echo X周前回显更新后的代码将按您的意愿执行。请参阅包含的3个示例。对于第一个回音,我在一周内收到1136289条回音,为什么?我想这与时间有关(),但不确定我是否意外地将其留在了一段调试代码中。我已删除
echo$time。“
如果您删除此项,它将删除
1周前的号码
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;
}

?>