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获取url的函数代码
Aug 02 PHP
php中长文章分页显示实现代码
Sep 29 PHP
php实现的click captcha点击验证码类实例
Sep 23 PHP
php网站被挂木马后的修复方法总结
Nov 06 PHP
php禁止浏览器使用缓存页面的方法
Nov 07 PHP
php设置静态内容缓存时间的方法
Dec 01 PHP
PHP递归创建多级目录
Nov 05 PHP
Yii2搭建后台并实现rbac权限控制完整实例教程
Apr 28 PHP
php mysql PDO 查询操作的实例详解
Sep 23 PHP
php中的explode()函数实例介绍
Jan 18 PHP
PHP常见的序列化与反序列化操作实例分析
Oct 28 PHP
WordPress伪静态规则设置代码实例
Dec 10 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+APACHE实现网址伪静态
2015/02/22 PHP
PHP+MySQL之Insert Into数据插入用法分析
2015/09/27 PHP
yii2 RBAC使用DbManager实现后台权限判断的方法
2016/07/23 PHP
php 二维数组时间排序实现代码
2016/11/19 PHP
PHP手机号中间四位用星号*代替显示的实例
2017/06/02 PHP
Nginx实现反向代理
2017/09/20 Servers
三个思路解决laravel上传文件报错:413 Request Entity Too Large问题
2017/11/13 PHP
30个最好的jQuery 灯箱插件分享
2011/04/25 Javascript
EXTJS记事本 当CompositeField遇上RowEditor
2011/07/31 Javascript
js function定义函数的几种不错方法
2014/02/27 Javascript
window.open不被拦截的简单实现代码(推荐)
2016/08/04 Javascript
jquery实时获取时间的简单实例
2017/01/26 Javascript
jquery mobile实现可折叠的导航按钮
2017/03/11 Javascript
JavaScript中使用webuploader实现上传视频功能(demo)
2017/04/10 Javascript
详解win7 cmd执行vue不是内部命令的解决方法
2017/07/27 Javascript
js DOM的事件常见操作实例详解
2019/12/16 Javascript
js实现页面图片消除效果
2020/03/24 Javascript
[45:17]DOTA2-DPC中国联赛定级赛 Phoenix vs DLG BO3第三场 1月9日
2021/03/11 DOTA
在Python中利用Into包整洁地进行数据迁移的教程
2015/03/30 Python
pycharm+PyQt5+python最新开发环境配置(踩坑)
2019/02/11 Python
python图像处理模块Pillow的学习详解
2019/10/09 Python
CSS3教程(3):border-color网页边框色彩
2009/04/02 HTML / CSS
Pretty You London官网:英国拖鞋和睡衣品牌
2019/05/08 全球购物
数百万免费的图形资源:Freepik
2020/09/21 全球购物
最新的大学生找工作自我评价
2013/09/29 职场文书
法律专业推荐信范文
2013/11/29 职场文书
奥巴马演讲稿
2014/01/08 职场文书
拾金不昧表扬信范文
2014/01/11 职场文书
班主任班级寄语大全
2014/04/04 职场文书
新颖的化妆品活动方案
2014/08/21 职场文书
个人主要事迹材料
2014/08/26 职场文书
初中差生评语
2014/12/29 职场文书
Go使用协程交替打印字符
2021/04/29 Golang
Django开发RESTful API实现增删改查(入门级)
2021/05/10 Python
mongodb清除连接和日志的正确方法分享
2021/09/15 MongoDB
js 实现Material UI点击涟漪效果示例
2022/09/23 Javascript