如何显示"12分钟前"等在PHP网页? [英] How to display "12 minutes ago" etc in a PHP webpage?

查看:144
本文介绍了如何显示"12分钟前"等在PHP网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can anyone tell me how I can display a status message like "12 seconds ago" or "5 minutes ago" etc in a web page?

推荐答案

以下是相同的php代码:

Here is the php code for the same:

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;
}

该函数以秒为单位输入并输出诸如以下内容的文本:

The function takes the number of seconds as input and outputs text such as:

  • 10秒
  • 1分钟

这篇关于如何显示"12分钟前"等在PHP网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆