php专区

 首页 > php专区 > PHP高级 > 日期 > php显示文章 几分钟前,几小时前,几天前发布类

php显示文章 几分钟前,几小时前,几天前发布类

分享到:
【字体:
导读:
          文章发表时的UNIX时间戳,来转化为例如,几分钟前,几小时前,几天前 这样的提示,如微博,这看起来更加人性化,类代码如下:?phpclasstimeAgo{static$timeagoObject;private$rustle;private$unit;priv...

php显示文章 几分钟前,几小时前,几天前发布类

文章发表时的UNIX时间戳,来转化为例如,几分钟前,几小时前,几天前....这样的提示,如微博,这看起来更加人性化,类代码如下:

  1.    class timeAgo 
  2.    {   
  3.       static $timeagoObject;    
  4.       private $rustle
  5.       private $unit
  6.        
  7.        private function __construct() 
  8.        { 
  9.            
  10.       }                 
  11.       private function __clone(){ } 
  12.       public static function getObject() 
  13.       { 
  14.           if(! (self::$timeagoObject instanceof self) ) 
  15.                  self::$timeagoObject = new timeAgo(); 
  16.                 
  17.           return self::$timeagoObject;   
  18.       } 
  19.       private function count_int($unix_C)   // main function 
  20.       { 
  21.           if(! (isset($unix_C) || is_numeric($unix_C)) ) 
  22.               return 'don't find parameter'; 
  23.                
  24.           $d = time()-$unix_C ;   // $d - unix time difference value  
  25.           $d_int =(int)floor($d/60) ; // minimum unit -- minutes   unix/60 
  26.           
  27.          $this->unit = 0 ;   // is minutes,hour or day? 
  28.            
  29.          if($d_int < 60){   // minutes   in one hour  3600 
  30.              $this->rustle = $d_int
  31.              $this->unit = 1;   
  32.              } 
  33.            else if($d_int < 720){  //hour    in one day  3600*12 
  34.                   $this->rustle = floor($d_int/60); 
  35.                 $this->unit = 2 ; 
  36.                   }    
  37.               else if($d_int < 7200){  //day  in ten days  3600*12*10 
  38.                        $this->rustle = floor($d_int/720); 
  39.                        $this->unit = 3 ; 
  40.                        } 
  41.                   else
  42.                        $this->rustle = $d ; 
  43.                       $this->unit = 4 ;    
  44.                       }  
  45.       } 
  46.      public function piece_str($C
  47.       { 
  48.          $this->count_int($C); 
  49.               
  50.            $u = '';  
  51.            switch$this->unit ) 
  52.            { 
  53.               case 1: 
  54.                    $u = 'minute'
  55.                    break
  56.               case 2: 
  57.                    $u = 'hour'
  58.                    break
  59.               case 3: 
  60.                    $u = 'day'
  61.                    break
  62.               case 4: 
  63.                    $u = ''
  64.                    break
  65.               case 0: 
  66.                    return 'sorry , get time is fail';      
  67.            } 
  68.            if($this->unit < 4) 
  69.            { 
  70.            if($this->rustle > 1) 
  71.                 return (string)$this->rustle.$u.'s ago'
  72.            else if($this->rustle == 1) 
  73.                   return (string)$this->rustle.$u.'ago'
  74.                else 
  75.                    return 'Just now';   
  76.            } 
  77.       } 
  78.       /*  example:   $ago = timeAgo::getObject(); 
  79.        *             echo $ago->piece_str($unix);     
  80.        *             // 2 days ago 
  81.       */ 
  82.   } 
  83. ?> 
分享到:
php中时间日期验证函数 - php日期
php中时间日期验证函数 本文章介绍了三个自定义函数,一个日期验证,一个时间验证,一个验证是否为时间和日期的,有需要的同学可以参考. 日期验证格式2011-12-12,代码如下: function is_date( $str )   {           $YEAR = "";           $MONTH = "";...
php 代码运行时间查看类 - php日期
php 代码运行时间查看类 class RunTime//页面执行时间类   {    private $starttime;//页面开始执行时间    private $stoptime;//页面结束执行时间    private $spendtime;//页面执行花费时间    function getmicrotime()//获取返回当前微秒数的浮点数   ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……