请问怎么把“多少小时前”精确到“多少小时多少分钟前”

2018-10-29 17:50:21 +08:00
 6680101

网站默认的时间格式,是“多少小时前”,觉得太单薄,想像 V2EX 这样,显示“多少小时多少分钟前”, 小白一个,请大神指教,多谢!

原代码如下:

function humandate($timestamp, $lan = array()) { $time = $_SERVER['time']; $lang = $_SERVER['lang'];

    static $custom_humandate = NULL;
    if($custom_humandate === NULL) $custom_humandate = function_exists('custom_humandate');
    if($custom_humandate) return custom_humandate($timestamp, $lan);

    $seconds = $time - $timestamp;
    $lan = empty($lang) ? $lan : $lang;
    empty($lan) AND $lan = array(
            'month_ago'=>'月前',
            'day_ago'=>'天前',
            'hour_ago'=>'小时前',
            'minute_ago'=>'分钟前',
            'second_ago'=>'秒前',
    );
    if($seconds > 31536000) {
            return date('Y-n-j', $timestamp);
    } elseif($seconds > 2592000) {
            return floor($seconds / 2592000).$lan['month_ago'];
    } elseif($seconds > 86400) {
            return floor($seconds / 86400).$lan['day_ago'];
    } elseif($seconds > 3600) {
            return floor($seconds / 3600).$lan['hour_ago'];
    } elseif($seconds > 60) {
            return floor($seconds / 60).$lan['minute_ago'];
    } else {
            return $seconds.$lan['second_ago'];
    }

}

4904 次点击
所在节点    PHP
27 条回复
tankeco
2018-10-30 00:31:28 +08:00
咦,时间那么复杂,难道不是<script src="moment.js" />么....?
6680101
2018-10-30 00:47:53 +08:00
@marcong95
非常感谢,我试试。

@feverzsj
哪里。lz 是做业务的。
这个是别人写的程序里的一小段。我觉得挺厉害的。
6680101
2018-10-30 00:55:01 +08:00
@tankeco

我又发现了新大陆。
kba977
2018-10-30 09:10:28 +08:00
这种活不应该直接交给 moment 么,一行代码的事
weirdo
2018-10-30 09:49:55 +08:00
@6680101 直接 carbon 啊,用时间戳或者格式化的时间 new 一个 carbon 对象,然后调 diffForHumans 方法,直接就出来了
realkaiway
2018-10-30 10:23:19 +08:00
时间处理为什么不考虑下 Moment.js...
6680101
2018-10-30 14:41:15 +08:00
@kba977
@weirdo
@realkaiway
@sobigfish

感谢提供思路。回头我再读读。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/502268

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX