国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php顯示文章 幾分鐘前,幾小時前,幾天前發布類

2024-09-04 11:45:33
字體:
來源:轉載
供稿:網友

文章發表時的UNIX時間戳,來轉化為例如,幾分鐘前,幾小時前,幾天前....這樣的提示,如微博,這看起來更加人性化,類代碼如下:

  1. <?php 
  2.    class timeAgo 
  3.    {   
  4.       static $timeagoObject;    
  5.       private $rustle
  6.       private $unit
  7.        
  8.        private function __construct() 
  9.        { 
  10.            
  11.       }                 
  12.       private function __clone(){ } 
  13.       public static function getObject() 
  14.       { 
  15.           if(! (self::$timeagoObject instanceof self) ) 
  16.                  self::$timeagoObject = new timeAgo(); 
  17.                 
  18.           return self::$timeagoObject;   
  19.       } 
  20.       private function count_int($unix_C)   // main function 
  21.       { 
  22.           if(! (isset($unix_C) || is_numeric($unix_C)) ) 
  23.               return 'don't find parameter'; 
  24.                
  25.           $d = time()-$unix_C ;   // $d - unix time difference value  
  26.           $d_int =(int)floor($d/60) ; // minimum unit -- minutes   unix/60 
  27.           
  28.          $this->unit = 0 ;   // is minutes,hour or day? 
  29.            
  30.          if($d_int < 60){   // minutes   in one hour  3600 
  31.              $this->rustle = $d_int
  32.              $this->unit = 1;   
  33.              } 
  34.            else if($d_int < 720){  //hour    in one day  3600*12 
  35.                   $this->rustle = floor($d_int/60); 
  36.                 $this->unit = 2 ; 
  37.                   }    
  38.               else if($d_int < 7200){  //day  in ten days  3600*12*10 
  39.                        $this->rustle = floor($d_int/720); 
  40.                        $this->unit = 3 ; 
  41.                        } 
  42.                   else
  43.                        $this->rustle = $d ; 
  44.                       $this->unit = 4 ;    
  45.                       }  
  46.       } 
  47.      public function piece_str($C
  48.       { 
  49.          $this->count_int($C); 
  50.               
  51.            $u = '';  
  52.            switch$this->unit ) 
  53.            { 
  54.               case 1: 
  55.                    $u = 'minute'
  56.                    break
  57.               case 2: 
  58.                    $u = 'hour'
  59.                    break
  60.               case 3: 
  61.                    $u = 'day'
  62.                    break
  63.               case 4: 
  64.                    $u = ''
  65.                    break
  66.               case 0: 
  67.                    return 'sorry , get time is fail';      
  68.            } 
  69.            if($this->unit < 4) 
  70.            { 
  71.            if($this->rustle > 1) 
  72.                 return (string)$this->rustle.$u.'s ago'
  73.            else if($this->rustle == 1) 
  74.                   return (string)$this->rustle.$u.'ago'
  75.                else 
  76.                    return 'Just now';   
  77.            } 
  78.       } 
  79.       /*  example:   $ago = timeAgo::getObject(); 
  80.        *             echo $ago->piece_str($unix);     
  81.        *             // 2 days ago 
  82.       */ 
  83.   } 
  84. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澄江县| 佛冈县| 丽江市| 武汉市| 凯里市| 弋阳县| 辽阳市| 甘洛县| 闸北区| 新化县| 南昌市| 泰兴市| 崇文区| 东辽县| 慈溪市| 新平| 乌鲁木齐县| 健康| 长顺县| 阆中市| 泰和县| 五家渠市| 盐津县| 康保县| 宁晋县| 兴安县| 微博| 鞍山市| 汝城县| 临桂县| 福泉市| 黄大仙区| 云南省| 崇州市| 南木林县| 华阴市| 昌黎县| 潢川县| 秦安县| 上杭县| 岐山县|