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 截取字符串 分别适合GB2312和UTF8编码情况
Feb 12 PHP
PHP排序算法的复习和总结
Feb 15 PHP
PHP中空字符串介绍0、null、empty和false之间的关系
Sep 25 PHP
php 修改、增加xml结点属性的实现代码
Oct 22 PHP
如何阻止网站被恶意反向代理访问(防网站镜像)
Mar 18 PHP
PHP中cookie和session的区别实例分析
Aug 28 PHP
ThinkPHP实现带验证码的文件上传功能实例
Nov 01 PHP
修改PHP脚本使WordPress拦截垃圾评论的方法示例
Dec 10 PHP
PHP通过CURL实现定时任务的图片抓取功能示例
Oct 03 PHP
PHP+iframe图片上传实现即时刷新效果
Nov 18 PHP
PHP排序算法之基数排序(Radix Sort)实例详解
Apr 21 PHP
用PHP的反射实现委托模式的讲解
Mar 22 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获取ip对应地区和使用网络类型的方法
2015/03/11 PHP
php获取flash尺寸详细数据的方法
2016/11/12 PHP
jquery实现当滑动到一定位置时固定效果
2014/06/17 Javascript
js防止页面被iframe调用的方法
2014/10/30 Javascript
JavaScript中的getTime()方法使用详解
2015/06/10 Javascript
jQuery获取父元素及父节点的方法小结
2016/04/14 Javascript
BootStrap 轮播插件(carousel)支持左右手势滑动的方法(三种)
2016/07/07 Javascript
利用jquery获取select下拉框的值
2016/11/23 Javascript
js 单引号替换成双引号,双引号替换成单引号的实现方法
2017/02/16 Javascript
vue生成token并保存到本地存储中
2018/07/17 Javascript
Vue异步组件处理路由组件加载状态的解决方案
2018/09/07 Javascript
详解将微信小程序接口Promise化并使用async函数
2019/08/05 Javascript
微信小程序 冒泡事件原理解析
2019/09/27 Javascript
JS实现多功能计算器
2020/10/28 Javascript
python基于xmlrpc实现二进制文件传输的方法
2015/06/02 Python
python字符串的常用操作方法小结
2016/05/21 Python
Python把csv数据写入list和字典类型的变量脚本方法
2018/06/15 Python
浅谈pandas筛选出表中满足另一个表所有条件的数据方法
2019/02/08 Python
Numpy之将矩阵拉成向量的实例
2019/11/30 Python
Django的CVB实例详解
2020/02/10 Python
python 图像插值 最近邻、双线性、双三次实例
2020/07/05 Python
20行Python代码实现一款永久免费PDF编辑工具的实现
2020/08/27 Python
以下为Windows NT 下的32 位C++程序,请计算sizeof 的值
2016/12/07 面试题
中考冲刺决心书
2014/03/11 职场文书
《大自然的语言》教学反思
2014/04/08 职场文书
中药学自荐信
2014/06/15 职场文书
工程安全生产协议书
2014/11/21 职场文书
幼儿教师小班个人总结
2015/02/05 职场文书
幼儿教师个人总结
2015/02/05 职场文书
怎样写辞职信
2015/02/27 职场文书
行政助理岗位职责范本
2015/04/11 职场文书
党支部鉴定意见
2015/06/02 职场文书
运动会5000米加油稿
2015/07/21 职场文书
2016年党员学习廉政准则心得体会
2016/01/20 职场文书
在 Golang 中实现 Cache::remember 方法详解
2021/03/30 Python
vue实力踩坑之push当前页无效
2022/04/10 Vue.js