php计算几分钟前、几小时前、几天前的几个函数、类分享


Posted in PHP onApril 09, 2014

一、函数实现
实例1:

function time_tran($the_time){
   $now_time = date("Y-m-d H:i:s",time()+8*60*60);
   $now_time = strtotime($now_time);
   $show_time = strtotime($the_time);
   $dur = $now_time - $show_time;
   if($dur < 0){
    return $the_time;
   }else{
    if($dur < 60){
     return $dur.'秒前';
    }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 $the_time;
       }
      }
 }

实例2:
<?php
function format_date($time){
    $t=time()-$time;
    $f=array(
        '31536000'=>'年',
        '2592000'=>'个月',
        '604800'=>'星期',
        '86400'=>'天',
        '3600'=>'小时',
        '60'=>'分钟',
        '1'=>'秒'
    );
    foreach ($f as $k=>$v)    {
        if (0 !=$c=floor($t/(int)$k)) {
            return $c.$v.'前';
        }
    }
}
?>

实例3:

function formatTime($date) {
$str = '';
$timer = strtotime($date);
$diff = $_SERVER['REQUEST_TIME'] - $timer;
$day = floor($diff / 86400);
$free = $diff % 86400;
if($day > 0) {
return $day."天前";
}else{
if($free>0){
$hour = floor($free / 3600);
$free = $free % 3600;
if($hour>0){
return $hour."小时前";
}else{
if($free>0){
$min = floor($free / 60);
$free = $free % 60;
if($min>0){
return $min."分钟前";
}else{
if($free>0){
return $free."秒前";
}else{
return '刚刚';
}
}
}else{
return '刚刚';
}
}
}else{
return '刚刚';
}
}
}

实例4:

function time_tran($the_time){
$now_time = date("Y-m-d H:i:s",time()+8*60*60); 
$now_time = strtotime($now_time);
$show_time = strtotime($the_time);
$dur = $now_time - $show_time;
if($dur < 0){
return $the_time; 
}else{
if($dur < 60){
    return $dur.'秒前'; 
}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 $the_time; 
   }
   }
    }
}
}
}

二、类的实现

<?php
/*
 * author: Solon Ring
 * time: 2011-11-02
 * 发博时间计算(年,月,日,时,分,秒)
 * $createtime 可以是当前时间
 * $gettime 你要传进来的时间
 */
class Mygettime{
        function  __construct($createtime,$gettime) {
            $this->createtime = $createtime;
            $this->gettime = $gettime;
    }
    function getSeconds()
    {
            return $this->createtime-$this->gettime;
        }
    function getMinutes()
       {
       return ($this->createtime-$this->gettime)/(60);
       }
      function getHours()
       {
       return ($this->createtime-$this->gettime)/(60*60);
       }
      function getDay()
       {
        return ($this->createtime-$this->gettime)/(60*60*24);
       }
      function getMonth()
       {
        return ($this->createtime-$this->gettime)/(60*60*24*30);
       }
       function getYear()
       {
        return ($this->createtime-$this->gettime)/(60*60*24*30*12);
       }
       function index()
       {
            if($this->getYear() > 1)
            {
                 if($this->getYear() > 2)
                    {
                        return date("Y-m-d",$this->gettime);
                        exit();
                    }
                return intval($this->getYear())." 年前";
                exit();
            }
             if($this->getMonth() > 1)
            {
                return intval($this->getMonth())." 月前";
                exit();
            }
             if($this->getDay() > 1)
            {
                return intval($this->getDay())." 天前";
                exit();
            }
             if($this->getHours() > 1)
            {
                return intval($this->getHours())." 小时前";
                exit();
            }
             if($this->getMinutes() > 1)
            {
                return intval($this->getMinutes())." 分钟前";
                exit();
            }
           if($this->getSeconds() > 1)
            {
                return intval($this->getSeconds()-1)." 秒前";
                exit();
            }
       }
  }
//类的使用实例
/*
 *
 * 调用类输出方式
 *
 * $a = new Mygettime(time(),strtotime('-25 month'));
 * echo iconv('utf-8', 'gb2312', $a->index())?iconv('utf-8', 'gb2312', $a->index()):iconv('utf-8', 'gb2312', '当前');
 *
 */
PHP 相关文章推荐
php 格式化数字的时候注意数字的范围
Apr 13 PHP
如何使用PHP实现javascript的escape和unescape函数
Jun 29 PHP
PHP验证码函数代码(简单实用)
Sep 29 PHP
PHP生成数组再传给js的方法
Aug 07 PHP
实例讲解yii2.0在php命令行中运行的步骤
Dec 01 PHP
可兼容php5与php7的cURL文件上传功能实例分析
May 11 PHP
yii2的restful api路由实例详解
May 14 PHP
thinkphp5实现微信扫码支付
Dec 23 PHP
Yii 框架入口脚本示例分析
May 19 PHP
phpstorm最新激活码分享亲测phpstorm2020.2.3版可用
Nov 22 PHP
ThinkPHP6.0如何利用自定义验证规则规范的实现登陆
Dec 16 PHP
PHP连接MSSQL数据库案例,PHPWAMP多个PHP版本连接SQL Server数据库
Apr 16 PHP
PHP扩展模块Pecl、Pear以及Perl的区别
Apr 09 #PHP
排序算法之PHP版快速排序、冒泡排序
Apr 09 #PHP
PHP读取大文件的类SplFileObject使用介绍
Apr 09 #PHP
php解决约瑟夫环示例
Apr 09 #PHP
适用于抽奖程序、随机广告的PHP概率算法实例
Apr 09 #PHP
PHP父类调用子类方法的代码例子
Apr 09 #PHP
一个基于phpQuery的php通用采集类分享
Apr 09 #PHP
You might like
example1.php
2006/10/09 PHP
Windows2003 下 MySQL 数据库每天自动备份
2006/12/21 PHP
php empty,isset,is_null判断比较(差异与异同)
2010/10/19 PHP
PHP mcrypt可逆加密算法分析
2011/07/19 PHP
PHP发送短信代码分享
2015/08/11 PHP
如何用PHP做到页面注册审核
2017/03/02 PHP
Windows上php5.6操作mongodb数据库示例【配置、连接、获取实例】
2019/02/13 PHP
javascript URL锚点取值方法
2009/02/25 Javascript
jQuery把表单元素变为json对象
2013/11/06 Javascript
JQuery中使用Ajax赋值给全局变量异常的解决方法
2014/01/10 Javascript
jquery操作复选框checkbox的方法汇总
2015/02/05 Javascript
JS实现统计复选框选中个数并提示确定与取消的方法
2015/07/01 Javascript
jQuery+ajax实现文章点赞功能的方法
2015/12/31 Javascript
快速掌握Node.js之Window下配置NodeJs环境
2016/03/21 NodeJs
jquery移除了live()、die(),新版事件绑定on()、off()的方法
2016/10/26 Javascript
微信小程序中显示html格式内容的方法
2017/04/25 Javascript
Node学习记录之cluster模块
2017/05/31 Javascript
vue1.0和vue2.0的watch监听事件写法详解
2018/09/11 Javascript
[01:02]DOTA2辉夜杯决赛日 CDEC.Y对阵VG赛前花絮
2015/12/27 DOTA
python执行等待程序直到第二天零点的方法
2015/04/23 Python
Python3使用requests包抓取并保存网页源码的方法
2016/03/15 Python
Python使用ctypes调用C/C++的方法
2019/01/29 Python
python中hasattr()、getattr()、setattr()函数的使用
2019/08/16 Python
Python 线程池用法简单示例
2019/10/02 Python
python应用Axes3D绘图(批量梯度下降算法)
2020/03/25 Python
python爬虫学习笔记之pyquery模块基本用法详解
2020/04/09 Python
Python中zip函数如何使用
2020/06/04 Python
Python CategoricalDtype自定义排序实现原理解析
2020/09/11 Python
HTML5表格_动力节点Java学院整理
2017/07/11 HTML / CSS
html5使用canvas画一条线
2014/12/15 HTML / CSS
机工车间主任岗位职责
2014/03/05 职场文书
大学生实习证明范本
2014/09/19 职场文书
自主招生专家推荐信
2015/03/26 职场文书
投诉书格式范本
2015/07/02 职场文书
在Centos 8.0中安装Redis服务器的教程详解
2022/03/21 Redis
解决Springboot PostMapping无法获取数据的问题
2022/05/06 Java/Android