• 根据时间转换今天昨天刚刚


    <?php

    /**
     * 根据时间得到 (今天,昨天...)
     * @param $date 2017-01-01
     */
    function getStrDate($the_time)
    {
        date_default_timezone_set("Asia/Shanghai");   //设置时区
        $now_time = time();//当前时间
        $show_time = $the_time;
        $dur = $now_time - $show_time;//两个时间的差距
        if ($dur < 0) {
            return date("Y-m-d H:i:s",$the_time);
        } else {
            if ($dur < 60) {
                return '刚刚';
            } else {
                if ($dur < 3600) {
                    return floor($dur / 60) . '分钟前';
                } else {
                    if ($dur < 86400) {
                        return floor($dur / 3600) . '小时前';
                    } else {
                        if ($dur < 259200) {//3天内
                            return floor($dur / 86400) . '天前';
                        } else {
                            return date("Y-m-d H:i:s",$the_time);
                        }
                    }
                }
            }
        }
    }

    /**
    * 将时间戳转为形式如:几天前、几月前、几分前、几秒前 刚刚
    * @param int $ctime 时间戳
    * @return string
    * @author Jacks(120041966@qq.com)
    */
    function formatCtime($ctime) {
    $time = time() - $ctime;
    $today = strtotime(date('Y-m-d', time()));
    $year = date('Y', time());
    $cyear = date('Y', $ctime);
    //1、1-5分钟内:刚刚
    if($year == $cyear) {
    if($time <= 300){
    $str = '刚刚';
    // 2、5-60分钟内:XX分钟前
    }
    else if($time <= 3600){
    $str = ceil($time / 60) . "分钟前";
    //3、1小时-今日结束前:今天
    }
    else if($ctime >= $today){
    $str = intval($time / 3600). '小时前';
    //4、昨天:昨天(如果用户是在半夜11点59分操作,操作完就到第二天了,也是显示昨天,而不是刚刚)
    }
    else {
    $str = date('m-d H:i', $ctime);
    }
    }
    else {
    $str = date('Y-m-d', $ctime);
    }
    return $str;
    }

    ?>
  • 相关阅读:
    System Idle Process SYSTEM占用CPU
    apache和nginx的rewrite的区别
    解决file_get_contents failed to open stream: HTTP request failed! 错误
    个人总结大型高并发高负载网站的系统架构(转)
    代码的抽象三原则
    mysqldump导入某库某表的数据
    mysql中insert into和replace into以及insert ignore用法区别
    【原创】学习日记4:nginx负载均衡(二)2012.01.08
    【原创】学习日记1:redis安装以及lnmp环境搭建2012.01.06
    mysql优化 mysql.ini优化
  • 原文地址:https://www.cnblogs.com/jhy-ocean/p/7478706.html
Copyright © 2020-2023  润新知