微信QQ发送消息时间提示

先上图: 看效果
微信QQ发送消息时间提示
微信QQ发送消息时间提示

代码如下:

//类似微信聊天记录 消息发送时间的提示
public function getChatTimeStr($addTime)
    {
        $nowTime = time();

        if ($addTime > $nowTime) {
            return "";
        }
        //返回的时间
        $timeStr = "";
        //获取当前时间
        $addTime = explode(',', date('Y,n,j,w,a,h,i,y', $addTime));//年,月,日,星期,上下午,时,分
        $nowTime = explode(',', date('Y,n,j,w,a,h,i,y', $nowTime));
        //print_r($addTime);exit;
        $dayPerMonthAddTime = self::getDayPerMonth($addTime[0]);
        $week = array(0 => "星期日", 1 => "星期一", 2 => "星期二", 3 => "星期三", 4 => "星期四", 5 => "星期五", 6 => "星期六");

        if ($addTime[4] == "am") {
            $am = " 上午";
        } else if ($addTime[4] == "pm") {
            $am = " 下午";
        }
        //如果时间差小于一天的,显示(上午 时间) / (下午 时间)
        if ($addTime[0] == $nowTime[0] && $addTime[1] == $nowTime[1] && $addTime[2] == $nowTime[2]) {
            $timeStr .= $am . ' ' . $addTime[5] . ":" . $addTime[6];
            //echo $timeStr;exit;
        } else if (($addTime[0] == $nowTime[0] && $addTime[1] == $nowTime[1] && $addTime[2] == $nowTime[2] - 1)
            || ($addTime[0] == $nowTime[0] && $nowTime[1] - $addTime[1] == 1 && $dayPerMonthAddTime[$addTime[1]] == $addTime[2] && $nowTime[2] == 1)
            || ($nowTime[0] - $addTime[0] == 1 && $addTime[1] == 12 && $addTime[2] == 31 && $nowTime[1] == 1 && $nowTime[2] == 1)) { //如果时间差在昨天,三种情况(同一月份内跨一天、月末跨越到月初、年末跨越到年初)显示格式:昨天 上午/下午  时:分


            $timeStr .= "昨天 " . $am . $addTime[5] . ":" . $addTime[6] . " ";
        } else if (($addTime[0] == $nowTime[0] && $addTime[1] == $nowTime[1] && $nowTime[2] - $addTime[2] < 7)
            || ($addTime[0] == $nowTime[0] && $nowTime[1] - $addTime[1] == 1 && $dayPerMonthAddTime[$addTime[1]] - $addTime[2] + $nowTime[2] < 7
                || ($nowTime[0] - $addTime[0] == 1 && $addTime[1] == 12 && $nowTime[1] == 1 && 31 - $addTime[2] + $nowTime[2] < 7))) { //如果时间差在一个星期之内的,也是三种情况,显示格式:星期 上午/下午 时:分

            $timeStr .= $week[$addTime[3]] . " " . $am . ' ' . $addTime[5] . ":" . $addTime[6];

        } else { //显示格式:年-月-日 上午/下午 时:分
            $timeStr .= $addTime[0] . "年" . $addTime[1] . "月" . $addTime[2] . "日 " . $am . ' ' . $addTime[5] . ":" . $addTime[6];
            //echo $timeStr;exit;
        }

        return $timeStr;

    }

辅助函数:

//根据年份获取每个月份的总天数和每年最后一个月的天数
    private function getDayPerMonth($year)
    {
        $arr = array(
            1 => 31,
            3 => 31,
            4 => 30,
            5 => 31,
            6 => 30,
            7 => 31,
            8 => 31,
            9 => 30,
            10 => 31,
            11 => 30,
            12 => 31
        );
        //闰年
        if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {
            $arr[2] = 29;
        } else {
            $arr[2] = 28;
        }
        return $arr;
    }

参考:https://blog.csdn.net/u014520745/article/details/52605189

上一篇:机器学习中的数据预处理---数据归一化


下一篇:标准化和归一化