ホームページ  >  記事  >  バックエンド開発  >  PHP は、数分前、数時間前、数日前に共有されたいくつかの関数とクラスを計算します_PHP チュートリアル

PHP は、数分前、数時間前、数日前に共有されたいくつかの関数とクラスを計算します_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:34:31772ブラウズ

1. 関数の実装
例 1:

コードをコピー コードは次のとおりです:

function time_tran($the_time){
$now_time = date("Y-m-d H:i:s",time( )+8 *60*60);
$now_time = strtotime($now_time);
$show_time = strtotime($the_time);
$dur = $now_time - $show_time;
if($dur return $the_time ;
}else{
if($dur return $dur.'秒前';
}else{
if($dur return Floor($dur/60) )数分前';
数日以内
return Floor($durコードをコピーします

コードは次のとおりです:

function format_date($time){
$t=time()-$ time;
$f=array(
'31536000'=>'年',
'2592000'=>'月',' '604800' = & gt; '週',
'86400' = & gt; '神',
'3600' = & gt; ',
' 60 '= & gt; =>'秒'
);
foreach ($f as $k=>$v) { if (0 != $ c = floor($ t/(int)$ k)){$ $ c。$ v.'front '; 例 3:

コードをコピー

コードは次のとおりです:


function formatTime($date) {
$str = '';
$timer = strtotime($date);
$diff = $ _SERVER[' REQUEST_TIME'] - $timer;
$day = Floor($diff / 86400);
$free = $diff % 86400;
if($day > 0) {
return $day."日前" ;
} else{
if($free>0){
$hour = Floor($free / 3600);
$free = $free % 3600;
if($hour>0){
$hour を返します。」時間前" ;}else{

if($free>0){
$min = Floor($free / 60);

$free = $free % 60;if($min>0){return $ min." 分前";}else{if($free>0){
return $free."秒前";
}else{
return 'just';
}
}
}else{
return 'ちょうど' ;
}
}
}else{
return 'ちょうど';
}
}
}



例 4:



コードをコピーします

コードは次のとおりです:


function time_tran($the_time){
$now_time = date("Y-m-d H:i:s",time()+8* 60*60) ;
$now_time = strtotime($now_time);
$show_time = strtotime($the_time);
$dur = $now_time - $show_time;
if($dur return $the_time;
}else{
if($dur return $dur.'秒前';
}else{
if($dur return Floor($dur/60).' 分前';
}else{ if($dur return Floor($dur/3600).'時間前';
}else{
if($dur return Floor( $dur/86400).'days ago'; }else{ return $the_time; 2. クラスの実装


コードをコピーします

コードは次のとおりです:
/*
* author: Solon Ring
* time: 2011-11-02
* 发博时间计算(年,月,日,时,分,秒)
* $createtime 可以是当前时间
* $gettime 你要传进来的时间
*/

class Mygettime{

function __construct($createtime,$gettime) {
$this->createtime = $createtime;
            $this->gettime = $gettime;
    }

    function getSeconds()
    {
            return $this->createtime-$this->gettime;
        }

    function getMinutes()
       {
       return ($this->createtime-$this->gettime)/(60);
       }

      function getHours()
       {
       return ($this->createtime-$this->gettime)/(60*60);
       }

      function getDay()
       {
        return ($this->createtime-$this->gettime)/(60*60*24);
       }

      function getMonth()
       {
        return ($this->createtime-$this->gettime)/(60*60*24*30);
       }

       function getYear()
       {
        return ($this->createtime-$this->gettime)/(60*60*24*30*12);
       }

       function index()
       {
            if($this->getYear() > 1)
            {
                 if($this->getYear() > 2)
                    {
                        return date("Y-m-d",$this->gettime);
                        exit();
                    }
                return intval($this->getYear())." 年前";
                exit();
            }

             if($this->getMonth() > 1)
            {
                return intval($this->getMonth())." 月前";
                exit();
            }

             if($this->getDay() > 1)
            {
                return intval($this->getDay())." 天前";
                exit();
            }

             if($this->getHours() > 1)
            {
                return intval($this->getHours())." 小时前";
                exit();
            }

}}

}

}

}
//クラスの使用例
/*
*
* クラスの出力メソッドを呼び出す

*

* $a = new Mygettime(time(),strtotime('-25 month'));

* echo iconv (' utf-8', 'gb2312', $a->index())?iconv('utf-8', 'gb2312', $a->index()):iconv('utf-8' 、' gb2312'、'現在');

*
*/






http://www.bkjia.com/PHPjc/751508.html

www.bkjia.com

tru​​e

http://www.bkjia.com/PHPjc/751508.html

1. 関数の実装例 1: 次のコードをコピーします。 function time_tran($the_time){ $now_time = date("Y-m-d H:i:s",time()+8*60*60); $ now_time); $show_time...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。