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 相关文章推荐
如何在WIN2K下安装PHP4.04
Oct 09 PHP
探讨:如何编写PHP扩展
Jun 13 PHP
php获取$_POST同名参数数组的实现介绍
Jun 30 PHP
详解PHP中strlen和mb_strlen函数的区别
Mar 07 PHP
Fatal error: session_start(): Failed to initialize storage module: files问题解决方法
May 04 PHP
PHP中filter函数校验数据的方法详解
Jul 31 PHP
详解在PHP的Yii框架中使用行为Behaviors的方法
Mar 18 PHP
PHP编写文件多服务器同步程序
Jul 02 PHP
微信公众平台开发(五) 天气预报功能开发
Dec 03 PHP
PHP缓存工具XCache安装与使用方法详解
Apr 09 PHP
TP5框架实现自定义分页样式的方法示例
Apr 05 PHP
PHP队列场景以及实现代码实例详解
Feb 26 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
一个自定义位数的php多用户计数器代码
2007/03/11 PHP
CodeIgniter基本配置详细介绍
2013/11/12 PHP
PHP实现的Redis多库选择功能单例类
2017/07/27 PHP
基于json的jquery地区联动效果代码
2011/07/06 Javascript
用jquery实现点击栏目背景色改变
2012/12/10 Javascript
JQuery入门——用映射方式绑定不同事件应用示例
2013/02/05 Javascript
IE6下opacity与JQuery的奇妙结合
2013/03/01 Javascript
javascript删除option选项的多种方法总结
2013/11/22 Javascript
JQuery boxy插件在IE中边角图片不显示问题的解决
2015/05/20 Javascript
jQuery的bind()方法使用详解
2015/07/15 Javascript
举例讲解JavaScript中将数组元素转换为字符串的方法
2015/10/25 Javascript
详解JavaScript语言的基本语法要求
2015/11/20 Javascript
JS中多步骤多分步的StepJump组件实例详解
2016/04/01 Javascript
Vue内容分发slot(全面解析)
2017/08/19 Javascript
es6数值的扩展方法
2019/03/11 Javascript
微信小程序如何引用外部js,外部样式,公共页面模板
2019/07/23 Javascript
将Vue组件库更换为按需加载的方法步骤
2020/05/06 Javascript
[02:46]解说DC:感谢430陪伴我们的DOTA2国际邀请赛岁月
2016/06/29 DOTA
python实现数通设备tftp备份配置文件示例
2014/04/02 Python
使用Turtle画正螺旋线的方法
2017/09/22 Python
详解Python下ftp上传文件linux服务器
2018/06/21 Python
django框架自定义用户表操作示例
2018/08/07 Python
Python判断telnet通不通的实例
2019/01/26 Python
Django框架中间件(Middleware)用法实例分析
2019/05/24 Python
Python中实现输入超时及如何通过变量获取变量名
2020/01/18 Python
python利用百度云接口实现车牌识别的示例
2020/02/21 Python
html5 datalist标签使用示例(自动完成组件)
2014/05/04 HTML / CSS
Bibloo匈牙利:女装、男装、童装及鞋子和配饰
2019/04/14 全球购物
关于VPN
2012/06/10 面试题
开工庆典邀请函范文
2014/01/16 职场文书
明信片寄语大全
2014/04/08 职场文书
4s店销售经理岗位职责
2014/07/19 职场文书
县委班子四风对照检查材料思想汇报
2014/09/29 职场文书
离婚起诉书范文2015
2015/05/19 职场文书
有关水浒传的读书笔记
2015/06/25 职场文书
Java中的随机数Random
2022/03/17 Java/Android