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;
}
对于技术来说,写这个东西,真没技术含量,只所以贴出了,是想表明我想做产品的态度
php时间区间,优化显示的更多相关文章
- pandas处理时间序列(2):DatetimeIndex、索引和选择、含有重复索引的时间序列、日期范围与频率和移位、时间区间和区间算术
一.时间序列基础 1. 时间戳索引DatetimeIndex 生成20个DatetimeIndex from datetime import datetime dates = pd.date_rang ...
- 2022-09-11-Typecho_RSS优化显示全文
layout: post cid: 26 title: Typecho RSS优化显示全文 slug: 26 date: 2022/09/11 15:53:38 updated: 2022/09/11 ...
- SQL Server 判定时间是否在某个时间区间内
* FROM sys.objects WHERE name=N'uF_IsRange_Date' AND [type]='FN') DROP FUNCTION uF_IsRange_Date GO S ...
- My97设置开始、结束 时间区间及输入框不能输入只能选择的方法
时间区间开始: <input type="text" id = "first_time" name="first_time" valu ...
- bootstrap时间区间设置方法
我们在开发过程中经常有时间区间的要求,在多次"失败"及翻阅资料之后终于找到了适合我的方法,所以给大家分享出来. 基本需求为可以设置时间,设置时间区间,后一时间日期不可提前于前一时间 ...
- Js:消息弹出框、获取时间区间、时间格式、easyui datebox 自定义校验、表单数据转化json、控制两个日期不能只填一个
(function ($) { $.messageBox = function (message) { $.messager.show({ title:'消息框提示', msg:message, sh ...
- JS------获取一个时间区间的所有天
1:获取一个时间区间的所有日期 function getDiffDate(start, end) { var startTime = getDate(start); var endTime = get ...
- 减少TIME_WAIT时间的优化配置
减少TIME_WAIT时间的优化配置 建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: net.ipv4.tcp_max_syn_backlog=8192 增加TCP S ...
- vue使用iview Timeline 时间轴不显示问题
vue Timeline 时间轴不显示渲染的效果 官网代码 <Timeline pending> <TimelineItem>发布1.0版本</TimelineItem& ...
- JAVA判断时间是否在时间区间内
package com.liying.tiger.test; import java.text.ParseException; import java.text.SimpleDateFormat; i ...
随机推荐
- 分享我的CleanArchitecture for Razor Page项目模板
这个项目是参考和整合了jasontaylordev/CleanArchitecture 和 blazorhero/CleanArchitecture 代码基础上,重构出来的新的项目,这两个项目都是非常 ...
- MySQL Replication Thread States
1.主节点线程状态(Replication Master Thread States): Finished reading one binlog; switching to next binlog 线 ...
- Spring Cloud Alibaba - Gateway
Gateway Gateway简介 底层使用Netty框架,性能大于Zuul 配置gateway模块,一般使用yaml格式: server: port: 80 #spring boot actuato ...
- Notepad++的NppFTP插件连接linux操作系统
Notepad++的NppFTP插件连接linux操作系统 下载地址:https://notepad-plus-plus.org/downloads/v8.1.2/ 1.安装Npp_FTP插件 两种方 ...
- Spring学习02(DI依赖注入)
5.依赖注入(Dependency Injection,DI) 5.1 概念 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 . 注入 : 指Bean对象所依赖的资源 , 由容器 ...
- spring中的组合模式
org.springframework.cache.support.CompositeCacheManager /* * Copyright 2002-2016 the original author ...
- springboot项目打包docker镜像maven插件
<!-- profile docker config --> <profiles> <profile> <id>docker</id> &l ...
- shodan搜索
扫描一切联网的设备 www.shodan.io 一.ip 直接搜索:123.123.123.123 二.搜索服务 http http country:"DE" 指定搜索德国 htt ...
- SQL 练习29
查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 SELECT Student.Sname,Course.Cname,SC.score FROM Student,Course,SC WHER ...
- MySQL自动填充
一.数据库级别 1.1 表设计 create_time默认值添加CURRENT_TIMESTAMP. update_time默认值添加CURRENT_TIMESTAMP,更新打勾. 1.2 验证是否成 ...