zoukankan      html  css  js  c++  java
  • 时间转换为今天昨天前天几天前

    经常在朋友圈,QQ空间、微博上看到动态的发布时间、评论时间,都显示,昨天,前天,几天前,比起直接显示几月几日几分几秒要优雅的多。

    于是自己的项目也想采用这种优雅直观的方式,网上找了各种计算相差几天的的例子,都是直接将时间戳相见除以86400,比如现在是17:08,动态更新的时间为前天22:00,这种方式计算的相差天数为1,而不是两天前。

    实际情况应该是,昨天任何时间都算一天前,前天任意时间都算2天前,所以自己琢磨了一番,去动态更新时间与今天23:59:59相差的时间秒数与86400(24 x 3600)相除后,向下取整,这样就得到了相差的天数,比如昨天00:00~昨天23:59:59的任何时间与今天的23:59:59,都相差 86400~(86400 x 2) 天,也就是2天。

    /**
     * 获取已经过了多久
     * PHP时间转换
     * 刚刚、几分钟前、几小时前
     * 今天昨天前天几天前
     * @param  string $targetTime 时间戳
     * @return string
     */
    function get_last_time($targetTime)
    {
        // 今天最大时间
        $todayLast   = strtotime(date('Y-m-d 23:59:59'));
        $agoTimeTrue = time() - $targetTime;
        $agoTime     = $todayLast - $targetTime;
        $agoDay      = floor($agoTime / 86400);
    
        if ($agoTimeTrue < 60) {
            $result = '刚刚';
        } elseif ($agoTimeTrue < 3600) {
            $result = (ceil($agoTimeTrue / 60)) . '分钟前';
        } elseif ($agoTimeTrue < 3600 * 12) {
            $result = (ceil($agoTimeTrue / 3600)) . '小时前';
        } elseif ($agoDay == 0) {
            $result = '今天 ' . date('H:i', $targetTime);
        } elseif ($agoDay == 1) {
            $result = '昨天 ' . date('H:i', $targetTime);
        } elseif ($agoDay == 2) {
            $result = '前天 ' . date('H:i', $targetTime);
        } elseif ($agoDay > 2 && $agoDay < 16) {
            $result = $agoDay . '天前 ' . date('H:i', $targetTime);
        } else {
            $format = date('Y') != date('Y', $targetTime) ? "Y-m-d H:i" : "m-d H:i";
            $result = date($format, $targetTime);
        }
        return $result;
    }
    private static String converToTimeString(Date time) {
            Date todayEndTime = DateUtils.getTodayEndTime();
            long betweenSecondes = DateUtils.betweenSecondes(todayEndTime, time);
            int agoDay = new Double(Math.floorDiv(betweenSecondes, 86400)).intValue();
    
            StringBuilder buffer = new StringBuilder(12);
            if (agoDay == 0) {
                buffer.append("今天 ");
            } else if (agoDay == 1) {
                buffer.append("昨天 ");
            } else if (agoDay == 2) {
                buffer.append("前天 ");
            } else {
                buffer.append(agoDay).append("天前 ");
            }
            buffer.append(DateUtils.convertDateToString(time, DateUtils.HH_MM_SS));
            return buffer.toString();
    }

    转自https://www.kancloud.cn/php-jdxia/jdxia-phpnote/450133

  • 相关阅读:
    HTTP请求报文与响应报文
    RTTI (Run-time type information) in C++
    Advanced C++ | Virtual Copy Constructor
    Virtual Destructor
    Advanced C++ | Virtual Constructor
    What happens when more restrictive access is given to a derived class method in C++?
    Can static functions be virtual in C++?
    Virtual functions in derived classes
    Default arguments and virtual function
    Multiple Inheritance in C++
  • 原文地址:https://www.cnblogs.com/gotodsp/p/12557412.html
Copyright © 2011-2022 走看看