Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.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
如何显示";“12分钟前”;PHP网页中的etc?_Php - Fatal编程技术网

如何显示";“12分钟前”;PHP网页中的etc?

如何显示";“12分钟前”;PHP网页中的etc?,php,Php,有人能告诉我如何在网页上显示“12秒前”或“5分钟前”等状态消息吗?以下是相同的php代码: function time_since($since) { $chunks = array( array(60 * 60 * 24 * 365 , 'year'), array(60 * 60 * 24 * 30 , 'month'), array(60 * 60 * 24 * 7, 'week'), array(60 * 60 *

有人能告诉我如何在网页上显示“12秒前”或“5分钟前”等状态消息吗?

以下是相同的php代码:

function time_since($since) {
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
        array(1 , 'second')
    );

    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
        if (($count = floor($since / $seconds)) != 0) {
            break;
        }
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
    return $print;
}
函数时间\u自($since){
$chunks=数组(
阵列(60*60*24*365,'年'),
数组(60*60*24*30,'月'),
数组(60*60*24*7,'周'),
数组(60*60*24,'天'),
数组(60*60,'小时'),
数组(60,'分钟'),
数组(1,'秒')
);
对于($i=0,$j=count($chunks);$i<$j;$i++){
$seconds=$chunks[$i][0];
$name=$chunks[$i][1];
如果(($count=floor($since/$seconds))!=0){
打破
}
}
$print=($count==1)?'1'。$name:“$count{$name}s”;
返回$print;
}
该函数将秒数作为输入并输出文本,例如:

  • 10秒
  • 1分钟

etc

PHP的
\DateTime::diff
返回一个对象,您可以通过公共
i
属性获取该对象的分钟数。

哦,别忘了用实际值更改这些乘法,这样就不会每次运行时都计算:)因为我很好奇,用评估的乘积替换乘法序列快了约1.2%。无论我如何更改日期和时间,它只显示
33分钟too@ErikEdgren我也有同样的问题,因为我发送的是日期而不是秒。试试这个:
time\u自(time()-strotime($datetime))
TWEAK WITH NOW。函数时间_-since($since){$chunks=array(array(60*60*24*365,'year')、array(60*60*24*30,'month')、array(60*60*24*7,'week')、array(60*60*24,'day')、array(60*60,'minute)、array(1,'sec');for($i=0,$j=count chunks($i<$j;$i++){$seconds=$chunks[$i][0];$name=$chunks[$i][1]$count=floor($since/$seconds);if($count!=0){break;}}if($count==1){return$print='1'.$name;}else if($count<1){return$print='just now';}else{return print=“$count{$name}s”;}这一点已经在中讨论过(尽管更多的是C#重点),answers中的示例代码应该很容易转换为PHP。有一个很好的jquery插件:timeago.jsI使用了这个插件的一个修改版本,我用$datetime2=new DateTime()替换了date_create行$datetime2->setTimestamp($timestamp);并删除了s字符串前的所有撇号
function timeAgo($timestamp){
    $datetime1=new DateTime("now");
    $datetime2=date_create($timestamp);
    $diff=date_diff($datetime1, $datetime2);
    $timemsg='';
    if($diff->y > 0){
        $timemsg = $diff->y .' year'. ($diff->y > 1?"'s":'');

    }
    else if($diff->m > 0){
     $timemsg = $diff->m . ' month'. ($diff->m > 1?"'s":'');
    }
    else if($diff->d > 0){
     $timemsg = $diff->d .' day'. ($diff->d > 1?"'s":'');
    }
    else if($diff->h > 0){
     $timemsg = $diff->h .' hour'.($diff->h > 1 ? "'s":'');
    }
    else if($diff->i > 0){
     $timemsg = $diff->i .' minute'. ($diff->i > 1?"'s":'');
    }
    else if($diff->s > 0){
     $timemsg = $diff->s .' second'. ($diff->s > 1?"'s":'');
    }

$timemsg = $timemsg.' ago';
return $timemsg;
}