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中的超全局变量
Oct 09 PHP
PHP实现图片简单上传
Oct 09 PHP
一个简单的PHP&amp;MYSQL留言板源码
Jul 19 PHP
PHP设计模式之装饰者模式
Feb 29 PHP
浅析PHP中的字符串编码转换(自动识别原编码)
Jul 02 PHP
PHP与javascript实现变量交互的示例代码
Jul 23 PHP
PHP Global定义全局变量使用说明
Aug 15 PHP
PHP实现自动登入google play下载app report的方法
Sep 23 PHP
thinkphp文件处理类Dir.class.php的用法分析
Dec 08 PHP
详谈php ip2long 出现负数的原因及解决方法
Apr 05 PHP
php微信扫码支付 php公众号支付
Mar 24 PHP
php传值和传引用的区别点总结
Nov 19 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
spl_autoload_register与autoload的区别详解
2013/06/03 PHP
php安装xdebug/php安装pear/phpunit详解步骤(图)
2013/12/22 PHP
php实现的DateDiff和DateAdd时间函数代码分享
2014/08/16 PHP
php简单日历函数
2015/10/28 PHP
php微信公众平台开发(一) 配置接口
2016/12/06 PHP
javascript 尚未实现错误解决办法
2008/11/27 Javascript
jQuery Autocomplete自动完成插件
2010/07/17 Javascript
使用pjax实现无刷新更改页面url
2015/02/05 Javascript
JS控制网页动态生成任意行列数表格的方法
2015/03/09 Javascript
js+html5实现canvas绘制镂空字体文本的方法
2015/06/05 Javascript
JavaScript实现点击单元格改变背景色的方法
2016/02/12 Javascript
AngularJS基础 ng-include 指令示例讲解
2016/08/01 Javascript
jQuery插件zTree实现更新根节点中第i个节点名称的方法示例
2017/03/08 Javascript
微信小程序服务器日期格式化问题
2020/01/07 Javascript
Python中函数参数设置及使用的学习笔记
2016/05/03 Python
django模型层(model)进行建表、查询与删除的基础教程
2017/11/21 Python
Python中使用支持向量机SVM实践
2017/12/27 Python
django输出html内容的实例
2018/05/27 Python
python计算阶乘和的方法(1!+2!+3!+...+n!)
2019/02/01 Python
python图像处理模块Pillow的学习详解
2019/10/09 Python
pycharm远程连接服务器并配置python interpreter的方法
2020/12/23 Python
python爬虫智能翻页批量下载文件的实例详解
2021/02/02 Python
Python列表的深复制和浅复制示例详解
2021/02/12 Python
Python 里最强的地图绘制神器
2021/03/01 Python
用HTML5制作一个简单的桌球游戏的教程
2015/05/12 HTML / CSS
芬兰攀岩、山地运动和户外活动用品购物网站:Bergfreunde
2016/10/06 全球购物
意大利体育用品和运动服网上商店:Maxi Sport
2019/09/14 全球购物
外语专业毕业生自我评价分享
2013/10/05 职场文书
学生会干部自荐信
2014/02/04 职场文书
社区志愿者活动总结
2014/06/26 职场文书
防灾减灾活动总结
2014/08/30 职场文书
拾金不昧表扬信
2015/01/16 职场文书
Go Gin实现文件上传下载的示例代码
2021/04/02 Golang
原生JavaScript实现简单五子棋游戏
2021/06/28 Javascript
Python Matplotlib库实现画局部图
2021/11/17 Python
DE1103使用报告
2022/04/05 无线电