>  기사  >  백엔드 개발  >  PHP에서 시차를 문자열로 변환하는 구현

PHP에서 시차를 문자열로 변환하는 구현

不言
不言원래의
2018-06-21 13:50:142043검색

이 글은 주로 PHP에서 시간차를 문자열로 변환하는 구현을 소개합니다. 이제 특정 참조 값이 있습니다. 필요한 친구가 이를 참조할 수 있습니다.

글이 저장될 때 UNIX를 전달합니다. 데이터베이스 및 게시됨 타임스탬프는 몇 분 전, 몇 시간 전, 며칠 전과 같은 프롬프트로 변환될 수 있습니다. 웨이보 좋아요

이게 더 사용자 친화적인 것 같군요. 그럼 코드를 시작해 보겠습니다.

<?php 
class timeAgo 
{ 
static $timeagoObject; 
private $rustle; 
private $unit; 
private function __construct() 
{ 
} 
private function __clone(){ } 
public static function getObject() 
{ 
if(! (self::$timeagoObject instanceof self) ) 
self::$timeagoObject = new timeAgo(); 
return self::$timeagoObject; 
} 
private function count_int($unix_C) // main function 
{ 
if(! (isset($unix_C) || is_numeric($unix_C)) ) 
return &#39;don\&#39;t find parameter&#39;; 
$d = time()-$unix_C ; // $d - unix time difference value 
$d_int =(int)floor($d/60) ; // minimum unit -- minutes unix/60 
$this->unit = 0 ; // is minutes,hour or day? 
if($d_int < 60){ // minutes in one hour 3600 
$this->rustle = $d_int; 
$this->unit = 1; 
} 
else if($d_int < 720){ //hour in one day 3600*12 
$this->rustle = floor($d_int/60); 
$this->unit = 2 ; 
} 
else if($d_int < 7200){ //day in ten days 3600*12*10 
$this->rustle = floor($d_int/720); 
$this->unit = 3 ; 
} 
else{ 
$this->rustle = $d ; 
$this->unit = 4 ; 
} 
} 
public function piece_str($C) 
{ 
$this->count_int($C); 
$u = &#39;&#39;; 
switch( $this->unit ) 
{ 
case 1: 
$u = &#39;minute&#39;; 
break; 
case 2: 
$u = &#39;hour&#39;; 
break; 
case 3: 
$u = &#39;day&#39;; 
break; 
case 4: 
$u = &#39;&#39;; 
break; 
case 0: 
return &#39;sorry , get time is fail&#39;; 
} 
if($this->unit < 4) 
{ 
if($this->rustle > 1) 
return (string)$this->rustle.$u.&#39;s ago&#39;; 
else if($this->rustle == 1) 
return (string)$this->rustle.$u.&#39;ago&#39;; 
else 
return &#39;Just now&#39;; 
} 
} 
/* example: $ago = timeAgo::getObject(); 
* echo $ago->piece_str($unix); 
* // 2 days ago 
*/ 
} 
?>

위 내용은 모두의 학습에 도움이 되기를 바랍니다. PHP 중국어 웹사이트로!

관련 권장 사항:

PHP의 숫자 배열에서 최대 및 최소 10개의 숫자를 찾는 방법에 대해

PHP에서 문자열을 가로채는 몇 가지 방법 요약

위 내용은 PHP에서 시차를 문자열로 변환하는 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.