php时间区间,优化显示

<?php
/**
 * 类似微信的时间显示
 * 规则是:今天的,显示几秒前,几分钟前,几小时前,昨天的显示昨天 上午 XX:XX
 * 再往前,本周的,显示周几+时间,再往前,本年的,显示月日+时间,再往前,显示年月日+时间
 * User: tu6ge
 * Date: 2016/7/21
 * Time: 15:21
 */

function timespan($time){
    $the_day_time = strtotime(date(‘Y-m-d‘,$time));
    $one_day_second = $time-$the_day_time;
    if($one_day_second<3600*6){
        $pre_name = ‘凌晨‘;
    }elseif($one_day_second<3600*12){
        $pre_name = ‘上午‘;
    }elseif($one_day_second<3600*18){
        $pre_name = ‘下午‘;
    }else{
        $pre_name = ‘晚上‘;
    }
    $now_time = time();
    $today_time = strtotime(date(‘Y-m-d‘));
    $yesterday_time = $today_time-86400;
    $now_week = date(‘w‘,$now_time);
    $now_week==0 && $now_week=7;
    $now_week_time = $today_time-($now_week-1)*86400;
    $now_year_time = strtotime(date(‘Y-01-01‘));

    $the_week = date(‘w‘,$time);
    $the_week==0 && $the_week=7;
    $week_pre_name = ‘‘;
    switch ($the_week){
        case 1:
            $week_pre_name = ‘一‘;
            break;
        case 2:
            $week_pre_name = ‘二‘;
            break;
        case 3:
            $week_pre_name = ‘三‘;
            break;
        case 4:
            $week_pre_name = ‘四‘;
            break;
        case 5:
            $week_pre_name = ‘五‘;
            break;
        case 6:
            $week_pre_name = ‘六‘;
            break;
        case 7:
            $week_pre_name = ‘日‘;
            break;
        default:
            $week_pre_name = ‘‘;
    }

    $test = ‘‘;
    if($time>=$now_time){
        //未来
        $test = ‘‘;
    }elseif($time>$today_time){
        //今天
        $diff_time = $now_time-$time;
        if($diff_time<60){
            $test = $diff_time.‘秒前‘;
        }elseif($diff_time<3600){
            $test = intval($diff_time/60).‘分钟前‘;
        }else{
            $test = intval($diff_time/3600).‘小时前‘;
        }
    }elseif($time>$yesterday_time){
        //昨天
        $test = ‘昨天 ‘.$pre_name.‘ ‘.date(‘H:i‘,$time);
    }elseif($time>$now_week_time){
        //这周内
        $test = ‘周‘.$week_pre_name.‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$time);
    }elseif($time>$now_year_time){
        //今年
        $test = date(‘m月d日‘,$time).‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$time);
    }else{
        //更早
        $test = date(‘Y月m月d日‘,$time).‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$time);
    }
    return $test;
}

$time = time()-2;
for($i=0;$i<30;$i++){
    echo timespan($time);
    echo ‘<br>‘;
    $time = $time-3600*6;
}

//########################上面是用函数实现的
//########################下面是用对象实现的

define(‘NOW_TIME‘,time());
define(‘TODAY_TIME‘,strtotime(date(‘Y-m-d‘,NOW_TIME)));
define(‘YESTERDAY_TIME‘,TODAY_TIME-86400);
$now_week = date(‘w‘,NOW_TIME);
$now_week==0 && $now_week=7;
define(‘NOW_WEEK_TIME‘,TODAY_TIME-($now_week-1)*86400);
define(‘NOW_YEAR_TIME‘,strtotime(date(‘Y-01-01‘)));

class timespan{
    public $time; //要计算的时间的时间戳
    const now_time=NOW_TIME;
    const today_time=TODAY_TIME;
    const yesterday_time = YESTERDAY_TIME;
    const now_week_time = NOW_WEEK_TIME;
    const now_year_time = NOW_YEAR_TIME;
    function __construct($time=null)
    {
        !empty($time) && $this->time = $time;
        $this->time = $time;
    }

    /**
     * 返回 上午,下午,晚上,凌晨
     * @param null $time
     * @return string
     */
    function get_pre_name($time=null){
        !empty($time) && $this->time = $time;
        $the_day_time = strtotime(date(‘Y-m-d‘,$this->time));
        $one_day_second = $this->time-$the_day_time;
        if($one_day_second<3600*6){
            $pre_name = ‘凌晨‘;
        }elseif($one_day_second<3600*12){
            $pre_name = ‘上午‘;
        }elseif($one_day_second<3600*18){
            $pre_name = ‘下午‘;
        }else{
            $pre_name = ‘晚上‘;
        }
        return $pre_name;
    }

    function get_week_name($time=null){
        !empty($time) && $this->time = $time;
        $the_week = date(‘w‘,$this->time);
        $the_week==0 && $the_week=7;
        $week_pre_name = ‘‘;
        switch ($the_week){
            case 1:
                $week_pre_name = ‘一‘;
                break;
            case 2:
                $week_pre_name = ‘二‘;
                break;
            case 3:
                $week_pre_name = ‘三‘;
                break;
            case 4:
                $week_pre_name = ‘四‘;
                break;
            case 5:
                $week_pre_name = ‘五‘;
                break;
            case 6:
                $week_pre_name = ‘六‘;
                break;
            case 7:
                $week_pre_name = ‘日‘;
                break;
            default:
                $week_pre_name = ‘‘;
        }
        return $week_pre_name;
    }
    function formate($time = null){
        !empty($time) && $this->time = $time;
        $test = ‘‘;
        if($this->time>=self::now_time){
            //未来
            $test = ‘‘;
        }elseif($this->time>self::today_time){
            //今天
            $diff_time = self::now_time-$this->time;
            if($diff_time<60){
                $test = $diff_time.‘秒前‘;
            }elseif($diff_time<3600){
                $test = intval($diff_time/60).‘分钟前‘;
            }else{
                $test = intval($diff_time/3600).‘小时前‘;
            }
        }elseif($this->time>self::yesterday_time){
            //昨天
            $pre_name = $this->get_pre_name();
            $test = ‘昨天 ‘.$pre_name.‘ ‘.date(‘H:i‘,$this->time);
        }elseif($this->time>self::now_week_time){
            //这周内
            $pre_name = $this->get_pre_name();
            $week_pre_name = $this->get_week_name();
            $test = ‘周‘.$week_pre_name.‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$this->time);
        }elseif($this->time>self::now_year_time){
            //今年
            $pre_name = $this->get_pre_name();
            $test = date(‘m月d日‘,$this->time).‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$this->time);
        }else{
            //更早
            $pre_name = $this->get_pre_name();
            $test = date(‘Y月m月d日‘,$this->time).‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$this->time);
        }
        return $test;
    }
}
//调用方法
$obj = new timespan();
for($i=0;$i<30;$i++){
    echo $obj->formate($time);
    echo ‘<br>‘;
    $time = $time-3600*6;
}
时间: 2024-10-02 00:08:35

php时间区间,优化显示的相关文章

廊坊一中考评:查看互评信息实现及优化显示

[前言] 为了培养更多的人,最近组长准备将廊坊一中交接给下版人员,从而让更多的人从中受益:但是在交接之前,我们需要根据上次验收提出的变动来完善系统. 我这次主要的任务是完善界面上的显示,基本上的任务分配是每个人完善之前做的模块,虽然有时间没打开系统,但是还是自己亲自做的,打开之后倍感亲切.在这次完善的界面的同时又将自己当初的实现给温习了一次. [实现] 需求:后台管理员在设置完互评信息后,需要对其设置的互评信息进行查看(支持模糊查询),同时支持删除. 下面主要说一下这部分最主要功能:查看互评信息

hdoj 2883 kebab 【时间区间离散化 + 最大流】

kebab Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1273    Accepted Submission(s): 532 Problem Description Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on

图的着色问题 带时间区间的图着色问题

安排会面问题,有几个研究组,不同的学生各自有自己感兴趣的若干个组想去看,每个组讨论时间为t,求总的最小的时间. 1.枚举颜色的可能 o((n-1)^n) 2.进行k着色,从1开始,看可能与否,不行把k加1然后再判断 3. 韦尔奇.鲍威尔法对图G进行着色 a)将图G中的结点按度数的递减次序排列 b)用第一种颜色对第一点着色,按排列次序,对前面的着色点不邻接的每一点用上同样的颜色 c)用第二种颜色对尚未着色的点重复(b),第三种继续. 4.有个叫 路线着色问题 还没看懂 5.有时间区间的图着色问题

SharePoint 列表优化显示WebParts

SharePoint 列表优化显示WebParts 在开发sharepoint中,经常遇到需要自定义显示列表中的一部分作为导航的内容, 如公告栏,新闻链接,最新动态等.... 我们通常需要显示一个列表的标题,并且限制字符长度, 外加一些条件,如按创建的时间倒序这样来显示最新的动态, 显示的时间也需要转换格式等... 那我们来看看原始的列表如下图: 修改后的效果: 接下来我们用SPD打开这个站点,在sitepages里面新建一个页面, 命名为allexception, 这是个空白的页面, 然后点击

减少TIME_WAIT时间的优化配置

减少TIME_WAIT时间的优化配置 建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: net.ipv4.tcp_max_syn_backlog=8192 增加TCP SYN队列长度,使系统可以处理更多的并发连接 net.ipv4.tcp_syncookies = 1 #表示开启SYN Cookies.当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭: net.ipv4.tcp_tw_reuse = 1 #表示开启重用.

Sql server 查询指定时间区间工作日数、休息日数等日期操作

1.查询指定时间区间的工作日 这个主要难点是法定节假日,国家的法定节假日每年都不一样,还涉及到调休,所以我们设计一个假日表.主要字段有年份,类型(是否调休),假期日期.如下: CREATE TABLE [dbo].[Holidays]( [ID] [int] IDENTITY(1,1) NOT NULL, [Holiday] [datetime2](7) NULL,--假期日期 [YearS] [char](4) NULL,--年份 [daytype] [int] NULL--类型 ) 添加好当

时间日期类--显示时间

<HTML> <HEAD> <TITLE>时间日期篇--显示日期</TITLE> </HEAD> <BODY bgcolor="#fef4d2" background="../images/background3.JPG" > <br> <br> <center> <font color="ffaafa">   <h2&g

获取指定时间区间作业运行情况

背景:数据库服务器定期重启,想知道重启期间对作业的影响.通俗点就是服务器在重启这段时间,有哪些作业计划要运行,重启后是否要手动执行这些作业?第一次重启的时候,按照最笨的方式,把所有作业看一遍,然后人为判断有哪些作业将受到影响,再根据作业具体代码,确定是否需手动执行.后来老大说要弄个过程出来,通过传入起止时间参数,返回区间内的作业计划.PS:参考各类资料,修改过很多遍,最后成型在6月初,很多细节上的修改自己也记不清楚了,一直懒得整理.先放上代码,以及效果图. 1 /****************

HDU 3572 【最大流 &amp;&amp; 时间区间建图】

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5398    Accepted Submission(s): 1742 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

hdu 2883 kebab(时间区间压缩 &amp;amp;&amp;amp; dinic)

kebab Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1243    Accepted Submission(s): 516 Problem Description Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on