一個會經常用到的時間轉換函數

一個論壇中的時間轉換函數 ,論壇中經常會有發帖時間的自動變動,比如:幾秒前,幾分鐘前,今天幾點幾分發的帖子,昨天幾點幾分發的帖子,今年及大於今年的時間發的帖子,下面整理一個時間戳轉換函數,特意發出來分享一下,也希望,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分鐘前

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章