一个会经常用到的时间转换函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jzm1963173402/article/details/81612175

一个论坛中的时间转换函数 ,论坛中经常会有发帖时间的自动变动,比如:几秒前,几分钟前,今天几点几分发的帖子,昨天几点几分发的帖子,今年及大于今年的时间发的帖子,下面整理一个时间戳转换函数,特意发出来分享一下,也希望,coder朋友们可以提出一些宝贵的建议!

//时间对比
    public function time_tran($the_time) {
        $now_time = time();
        $wee_hours = strtotime(date('Y-m-d',time()));
        $yesterday = $wee_hours - 24*60*60;
        $dur = $now_time - $the_time;
        if ($dur < 0) {
            return $the_time;
        } else {
            if ($dur < 60) {
                return $dur . '秒前';
            } else {
                if ($dur < 3600) {
                    return floor($dur / 60) . '分钟前';
                } else {
                    if ($the_time > $wee_hours) {
                        return "今天 ".date("H:i",$the_time);
                    } else {
                        if ($the_time > $yesterday) {// 昨天
                            return '昨天 '.date("H:i",$the_time);
                        } else {
                            if ($the_time > mktime(0,0,0,1,1,date('Y'))) { //今年
                                return date("m月d日 H:i",$the_time);
                            } else {
                                return date("Y年m月d日 H:i",$the_time);
                            }

                        }
                    }
                }
            }
        }
    }

举几个��:
昨天 13:37
08月09日 10:39
7秒前
2017年12月12日 09:46
今天 20:59
1分钟前

猜你喜欢

转载自blog.csdn.net/jzm1963173402/article/details/81612175