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 相关文章推荐
第五节 克隆 [5]
Oct 09 PHP
PHP 动态随机生成验证码类代码
Apr 09 PHP
paypal即时到账php实现代码
Nov 28 PHP
PHP5权威编程阅读学习笔记 附电子书下载
Jul 05 PHP
有关phpmailer的详细介绍及使用方法
Jan 28 PHP
基于php-fpm的配置详解
Jun 03 PHP
php使用curl检测网页是否被百度收录的示例分享
Jan 31 PHP
php中in_array函数用法分析
Nov 15 PHP
Sublime里直接运行PHP配置方法
Nov 28 PHP
php array_keys 返回数组的键名
Oct 25 PHP
详解thinkphp5+swoole实现异步邮件群发(SMTP方式)
Oct 13 PHP
php+laravel依赖注入知识点总结
Nov 04 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连接MySQL查询结果中文显示乱码解决方法
2013/10/25 PHP
php bootstrap实现简单登录
2016/03/08 PHP
PHP创建多级目录的两种方法
2016/10/28 PHP
/etc/php-fpm.d/www.conf 配置注意事项
2017/02/04 PHP
php-7.3.6 编译安装过程
2020/02/11 PHP
奉献给JavaScript初学者的编写开发的七个细节
2011/01/11 Javascript
元素的内联事件处理函数的特殊作用域在各浏览器中存在差异
2011/01/12 Javascript
最简单的js图片切换效果实现代码
2011/09/24 Javascript
单击按钮显示隐藏子菜单经典案例
2013/01/04 Javascript
利用了jquery的ajax实现二级联互动菜单
2013/12/02 Javascript
超级简单的jquery操作表格方法
2014/12/15 Javascript
jQuery实现仿Google首页拖动效果的方法
2015/05/04 Javascript
JS仿淘宝实现的简单滑动门效果代码
2015/10/14 Javascript
bootstrap table动态加载数据示例代码
2017/03/25 Javascript
JS二分查找算法详解
2017/11/01 Javascript
JavaScript实现微信红包算法及问题解决方法
2018/04/26 Javascript
vue项目webpack中Npm传递参数配置不同域名接口
2018/06/15 Javascript
vue router的基本使用和配置教程
2018/11/05 Javascript
使用xampp将angular项目运行在web服务器的教程
2019/09/16 Javascript
Angular单元测试之事件触发的实现
2020/01/20 Javascript
python 字符串split的用法分享
2013/03/23 Python
Python编程实现二叉树及七种遍历方法详解
2017/06/02 Python
AI人工智能 Python实现人机对话
2017/11/13 Python
使用python3实现操作串口详解
2019/01/01 Python
Python Pandas 转换unix时间戳方式
2019/12/07 Python
在tensorflow中实现屏蔽输出的log信息
2020/02/04 Python
Django model.py表单设置默认值允许为空的操作
2020/05/19 Python
Python ConfigParser模块的使用示例
2020/10/12 Python
捷克多品牌在线时尚商店:ANSWEAR.cz
2020/10/03 全球购物
大三预备党员入党思想汇报
2014/01/08 职场文书
开发房地产协议书
2014/09/14 职场文书
物业项目经理岗位职责
2015/04/01 职场文书
关于应聘教师的自荐信
2016/01/28 职场文书
2016年秋季趣味运动会开幕词
2016/03/04 职场文书
安全责任协议书范本
2016/03/23 职场文书
Java实现二维数组和稀疏数组之间的转换
2021/06/27 Java/Android