时间转字符
2023-09-19
  function time2date($time){
        $text = '';
        $time = !$time || $time > time() ? time() : intval($time);
        $t = time() - $time; //时间差 (秒)
        $y = date('Y', $time)-date('Y', time());//是否跨年
        switch($t){
//            case $t == 0:
//                $text = '刚刚';
//                break;
            case $t < 60:
                $text = '刚刚'; // 一分钟内
                break;
            case $t < 60 * 60:
                $text = floor($t / 60) . '分钟前'; //一小时内
                break;
            case $t < 86400:
                $text = floor($t / (60 * 60)) . '小时前'; // 一天内
                break;
            case $t < 86400 * 3:
                $text = floor($time/(86400)) == 1 ?'昨天 '  : '前天 ' ; //昨天和前天
                break;
            case $t < 86400 * 30:
                $text = date('m月d日 H:i', $time); //一个月内
                break;
            case $t < 86400 * 365 && $y==0:
                $text = date('m月d日', $time); //一年内
                break;
            default:
                $text = date('Y年m月d日', $time); //前一年
                break;
        }
        return $text;
    }