比如要显示:XXX天后过期,XXX小时前已过期
使用时用 diffDate 方法

...


    public static function diffDate($timestampNow, $timestamp, $returnTimestamp = false, $level=0, $lang='zh')
    {
        if (!is_numeric($timestampNow)) $timestampNow = strtotime($timestampNow);
        if (!is_numeric($timestamp)) $timestamp = strtotime($timestamp);
        if ($timestamp > $timestampNow) $append = '后';
        elseif ($timestamp < $timestampNow) $append = '前';
        $diffTimestamp = $timestampNow - $timestamp;
        if ($returnTimestamp) return $diffTimestamp;
        $diffTimestamp = abs($diffTimestamp);
        if ($diffTimestamp > 86400) $level = 3;
        elseif ($diffTimestamp > 3600) $level = 2;
        else $level = 1;
        return self::timeLength($diffTimestamp, $lang, $level) . $append;
    }


    /**
     * 时长人性化显示
     * @param $second
     * @param $lang
     * @param int $level 返回的最小单位,0秒,1分,2时,3天
     * @return string
     */
    public static function timeLength($second, $lang='zh', $level=0)
    {
        $arr = [
            'zh'=>['天'=>'天','小时'=>'小时','分'=>'分',''=>'','秒'=>'秒',],
            'en'=>['天'=>'d','小时'=>'h','分'=>'m',''=>'','秒'=>'s',],
        ];
        $second = (int)$second;
        if ($second <=0 ) $second=0;
        $text = [];
        if ($level <= 3 && ($tmp1 = floor($second / 86400)) > 0) $text[] = "{$tmp1}{$arr[$lang]['天']}";
        if ($level <= 2 && ($tmp2 = floor(($second - $tmp1 * 86400) / 3600)) > 0) $text[] = "{$tmp2}{$arr[$lang]['小时']}";
        if ($level <= 1 && ($tmp3 = floor(($second - $tmp1 * 86400 - $tmp2 * 3600) / 60)) > 0) $text[] = "{$tmp3}{$arr[$lang]['分']}";
        if ($level <= 0 && ($tmp4 = $second % 60) > 0) $text[] = "{$tmp4}{$arr[$lang]['秒']}";
        return implode('', $text);
    }


    /**
     * 格式化时间戳
     * @param type $date
     * @return string
     */
    public static function formatTime($date) {
        $thisyear = intval(zmf::time(NULL, 'Y'));
        $dateyear = intval(zmf::time($date, 'Y'));
        if (($thisyear - $dateyear) > 0) {
            return zmf::time($date, 'Y-m-d H:i');
        }
        $thismo = intval(zmf::time(NULL, 'm'));
        $datemo = intval(zmf::time($date, 'm'));
        if ($thisyear == $dateyear && $thismo != $datemo) {
            return zmf::time($date, 'm-d H:i');
        }
        $timer = $date;
        $diff = zmf::now() - $timer;
        $thisto = intval(zmf::time(NULL, 'd'));
        $dateto = intval(zmf::time($date, 'd'));
        $day = $thisto - $dateto;
        $free = $diff % 86400;
        if ($day > 0) {
            if ($day > 7) {
                return zmf::time($date, 'n-j H:i');
            } elseif ($day == 1) {
                return "昨天 " . zmf::time($date, 'H:i');
            } elseif ($day == 2) {
                return "前天 " . zmf::time($date, 'H:i');
            } else {
                return $day . "天前";
            }
        } else {
            if ($free > 0) {
                $hour = floor($free / 3600);
                $free = $free % 3600;
                if ($hour > 0) {
                    return $hour . "小时前";
                } else {
                    if ($free > 0) {
                        $min = floor($free / 60);
                        $free = $free % 60;
                        if ($min > 0) {
                            return $min . "分钟前";
                        } else {
                            if ($free > 0) {
                                return $free . "秒前";
                            } else {
                                return '刚刚';
                            }
                        }
                    } else {
                        return '刚刚';
                    }
                }
            } else {
                return '刚刚';
            }
        }
    }


...

标签: none

添加新评论