Home > Backend Development > PHP Tutorial > Format chat list time

Format chat list time

angryTom
Release: 2023-04-08 07:18:02
forward
3486 people have browsed it

Format chat list time

Recently implemented a function similar to customer service chat.

But I don’t know much about the time format that needs to be displayed in the list

So, refer to the time display logic of the WeChat chat list. I implemented a function myself

Specific rules:

If you need to format the timestamp (t)> the early morning timestamp of the day, then display "AM/PM xx:xx" if t> If the timestamp in the early morning of yesterday is "yesterday", if t> the early morning timestamp of this Monday, then "week x" will be displayed. If t> the early morning timestamp of last Monday, "last week x" will be displayed if tPHP Tutorial"

Specific code:

/**
     * 格式化聊天列表时间
     * @param $timestamp int 时间戳
     * @return false|string
     */
    public static function formatChatListTime($timestamp){
        $today = strtotime('today');
        $yesterday = strtotime('yesterday');
        // 本周一
        $thisMonday = $today - ((date('w',time()) == 0 ? 7 : date('w',time()))-1)*24*3600;
        // 上周一
        $lastMonday = $thisMonday - 7*24*3600;

        if ($timestamp > $today){
            $a = date('a', $timestamp);
            $t = date('h:i', $timestamp);
            if ($a == 'am'){
                $a = '上午 ';
            }else{
                $a = '下午 ';
            }
            $result = $a.$t;
        }else if ($timestamp > $yesterday){
            $result = '昨天';
        }else if ($timestamp > $thisMonday){
            $result = self::getWeekDesc($timestamp);
        }else if ($timestamp > $lastMonday){
            $result = '上' . self::getWeekDesc($timestamp);
        }else{
            if (date('Y', $timestamp) == date('Y', time())){
                $result = self::dateTimeFormat($timestamp, 'm月d日');
            }else{
                $result = self::dateTimeFormat($timestamp, 'Y年m月d日');
            }
        }
        return $result;
    }


/**
 * 获取指定时间戳的星期几-中文描述
 * @param int $timeStamp 时间戳
 * @return string
 */
public static function getWeekDesc($timeStamp){
    if(intval($timeStamp) == 0){
        return '';
    }
    $week = date('w', $timeStamp);
    switch ($week){
        case 0:
            $desc = '星期日';
            break;
        case 1:
            $desc = '星期一';
            break;
        case 2:
            $desc = '星期二';
            break;
        case 3:
            $desc = '星期三';
            break;
        case 4:
            $desc = '星期四';
            break;
        case 5:
            $desc = '星期五';
            break;
        case 6:
            $desc = '星期六';
            break;
        default:
            $desc = '';
            break;
    }
    return $desc;
}
Copy after login

This article comes from the PHP Tutorial column, welcome to learn.

The above is the detailed content of Format chat list time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template