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 相关文章推荐
利用文件属性结合Session实现在线人数统计
Oct 09 PHP
php 正则匹配函数体
Aug 25 PHP
PHP的变量类型和作用域详解
Mar 12 PHP
php实现读取超大文件的方法
Jul 28 PHP
php不使用copy()函数复制文件的方法
Mar 13 PHP
php 读取输出其他文件的实现方法
Jul 26 PHP
PHP+JS实现的商品秒杀倒计时用法示例
Nov 15 PHP
PHP调用Mailgun发送邮件的方法
May 04 PHP
PHP实现对文件锁进行加锁、解锁操作的方法
Jul 04 PHP
PHP中soap用法示例【SoapServer服务端与SoapClient客户端编写】
Dec 25 PHP
ThinkPHP5.0框架结合Swoole开发实现WebSocket在线聊天案例详解
Apr 02 PHP
PHP使用Redis队列执行定时任务实例讲解
Mar 24 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
用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤
2014/01/19 PHP
设置php页面编码的两种方法示例介绍
2014/03/03 PHP
php实现的css文件背景图片下载器代码
2014/11/11 PHP
基于PHP实现等比压缩图片大小
2016/03/04 PHP
PHP编程实现的TCP服务端和客户端功能示例
2018/04/13 PHP
php微信开发之关键词回复功能
2018/06/13 PHP
php+js实现点赞功能的示例详解
2020/08/07 PHP
JS动画效果代码3
2008/04/03 Javascript
javascript 动态数据下的锚点错位问题解决方法
2008/12/24 Javascript
js Date自定义函数 延迟脚本执行
2010/03/10 Javascript
javascript 设为首页与加入收藏兼容多浏览器代码
2011/01/11 Javascript
jQuery EasyUI API 中文文档 - Parser 解析器
2011/09/29 Javascript
js图片延迟加载的实现方法及思路
2013/07/22 Javascript
javascript中怎么做对象的类型判断
2013/11/11 Javascript
javascript实现存储hmtl字符串示例
2014/04/25 Javascript
文本框倒叙输入让输入框的焦点始终在最开始的位置
2014/09/01 Javascript
浅谈Javascript事件对象
2017/02/05 Javascript
Vue修改项目启动端口号方法
2019/11/07 Javascript
vue 页面回退mounted函数不执行的解决方案
2020/07/26 Javascript
Python操作Redis之设置key的过期时间实例代码
2018/01/25 Python
Centos部署django服务nginx+uwsgi的方法
2019/01/02 Python
python rsync服务器之间文件夹同步脚本
2019/08/29 Python
Python 读取xml数据,cv2裁剪图片实例
2020/03/10 Python
Python3+Flask安装使用教程详解
2021/02/16 Python
python实现计算图形面积
2021/02/22 Python
罗技英国官方网站:Logitech UK
2020/11/03 全球购物
英文简历中的自我评价用语
2013/12/09 职场文书
高校教师岗位职责
2014/03/18 职场文书
护士求职自荐信范文
2014/03/19 职场文书
行政专员求职信范文
2014/05/03 职场文书
财务负责人任命书
2014/06/06 职场文书
单位作风建设自查报告
2014/10/23 职场文书
英文商务邀请函范文
2015/01/31 职场文书
单位车辆管理制度
2015/08/05 职场文书
唱歌比赛拉拉队口号
2015/12/25 职场文书
2016年小学教师政治学习心得体会
2016/01/23 职场文书