PHPで時差を文字列プロンプトに変換する実装コード

高洛峰
リリース: 2023-03-01 11:46:01
オリジナル
1061 人が閲覧しました

如微博
这看起来更人性化,好吧,上代网
代码如下:
class timeAgo
{
static $timeagoObject; 
プライベート$カサカサ; 
プライベート $ユニット; 

プライベート関数 __construct()
{

}
プライベート関数 __clone(){ }
パブリック静的関数 getObject()
{
if(! (self::$timeagoObject instanceof self) )
self::$timeagoObject =新しい時間前(); 

return self::$timeagoObject; 
}
プライベート関数 count_int($unix_C) // メイン関数
{
if(! (isset($unix_C) || is_numeric($unix_C)) )
return 'パラメータが見つかりません'; 

$d = time()-$unix_C ; // $d - UNIX 時差値
$d_int =(int)floor($d/60) ; // 最小単位 -- 分 unix/60

$this->unit = 0 ; // 分、時間、それとも日ですか? 

if($d_int < 60){// 1 時間の分 3600
$this->rustle = $d_int; 
$this->単位 = 1; 
}
else if($d_int < 720){///1 日の時間 3600*12
$this->rustle = Floor($d_int/60); 
$this->単位 = 2 ; 
}
else if($d_int < 7200){///10 日後の日 3600*12*10
$this->rustle = Floor($d_int/720); 
$this->ユニット = 3 ; 
}
else{
$this->カサカサ = $d ; 
$this->ユニット = 4 ; 
}
}
パブリック関数 Piece_str($C)
{
$this->count_int($C); 

$u = ''; 
switch( $this->unit )
{
case 1:
$u = '分'; 
休憩; 
ケース 2:
$u = '時間'; 
休憩; 
ケース 3:
$u = '日'; 
休憩; 
ケース 4:
$u = ''; 
休憩; 
ケース 0:
return '申し訳ありません、時間の取得は失敗しました'; 
}
if($this->unit < 4)
{
if($this->rustle > 1)
return (string)$this->rustle.$u. の前'; 
else if($this->rustle == 1)
return (string)$this->rustle.$u.'ago'; 
else
return '今だけ'; 
}
}
/* 例: $ago = timeAgo::getObject(); 
* echo $ago->piece_str($unix); 
* // 2 日前
*/
}
?> 

関連ラベル:
php
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!