首頁  >  文章  >  後端開發  >  php中將時間差轉換為字串提示的實作程式碼

php中將時間差轉換為字串提示的實作程式碼

高洛峰
高洛峰原創
2016-11-30 11:06:531063瀏覽

如微博
這看起來比較人性化,好吧,上碼
程式碼如下:
class timeAgo
{
static $timeagoObject; 
私人$沙沙; 
私人$單位; 

私有函數__construct() 

;

if(! (self::$timeagoObject instanceof self) )
self::$timeagoObject =新的時間(); 

回傳self::$timeago主函數 

if(! (isset($unix_C) || is_numeric($unix_C)) ) 
return '找不到參數'; 

$d = time()-$unix_CC // $d - unixix ;時間差值
$d_int =(int)floor($d/60) ; // 最小單位-- 分鐘unix/60 

$this->unit = 0 ; // 是分鐘、小時還是天?

if($d_int $this->rustle = $d_int; 
$this->unit = 1; 

else if($d_int $this->rustle = Floor($d_int/60); 
$this->unit = 2 ; 

else if($d_int $this->rustle = Floor($d_int/720); 
$this->unit = 3 ; 

else{ 
$this->沙聲= $dthis ; 
else{ 
$this->沙聲= $dthis ; 
else{ 
$this->沙聲= $dthis ; >unit = 4 ; 


公共函數piece_str($C) 

$this->count_int($C); 

$u = ''; 
itch(this-g狀況1: 
$u = '分鐘'; 
休息; 
狀況2:
$u = '小時'; 
休息; 
狀況3:
$u = '天'; 
休息; 
狀況3:
$u = '天'; 
休息; u = ''; 
休息; 
case 0:
return '抱歉,取得時間失敗'; 

if($this->unit { 
if($this->otle > 1) 

if($this-o>tle > 1) (string)$this->rustle.$u.'; 
else if($this->rustle == 1) 
return (string)$this->rustle.$u.'ago'; 
else 
return ' Just now'; 


/* 例:$ago = timeAgo::getObject(); 
* echo $ago->piece_str($unix); 
* // 2 天前 
*/ 
*/ 
*? > 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn