Home  >  Article  >  Backend Development  >  PHP calculates several functions and classes shared minutes ago, hours ago, and days ago_PHP Tutorial

PHP calculates several functions and classes shared minutes ago, hours ago, and days ago_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:31772browse

1. Function implementation
Example 1:

Copy code The code is as follows:

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 < 0){
return $the_time;
}else{
if($dur < 60){
return $dur.'seconds ago';
}else{
if($dur < 3600){
return floor($dur/60).'minutes ago';
}else{
}else{
 if($dur < 86400){
  return floor($dur/3600).'hours ago';
}else{
 if($dur < 259200){//Within 3 days
          return floor($dur/86400).'days ago'; 🎜>

Example 2:


Copy code

The code is as follows:
function format_date( $time){ $t=time()-$time; $f=array(
'31536000'=>'year',
'2592000'=>'month ',
'604800'=>'week',
'86400'=>'day',
'3600'=>'hour',
'60'=> 'Minutes',
'1'=>'Seconds'
);
foreach ($f as $k=>$v) {
if (0 !=$c=floor ($ t/(int) $ k)) {
Return $ C. $ v.
Example 3:



Copy code

The code is as follows:

function formatTime($date) {$str = '';

$timer = strtotime($date);
$diff = $_SERVER['REQUEST_TIME'] - $timer;

$day = floor($diff / 86400); $free = $diff % 86400;if($day > 0) {return $day."days ago";}else{
if($free>0){
$hour = floor($free / 3600);
$free = $free % 3600;
if($hour>0){
return $hour."hour ago";
}else{
if($free>0){
$min = floor($free / 60);
$free = $free % 60;
if($min>0){
return $min."minutes ago";
}else{
if($free>0){
return $free."seconds ago";
}else{
return 'just';
}
}
}else{
return 'just';
}
}
}else{
return 'just';
}
}
}



Example 4:



Copy code

The code is as follows:

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 < 0){return $the_time; }else{if($dur < 60){
return $dur.'seconds ago';
}else{
if($dur < 3600){
return floor($dur/60).'minutes ago';
}else{
if($dur < 86400){
return floor($dur/3600).'hours ago';
}else{
if($dur < 259200){//Within 3 days
return floor($dur/86400).'day';
}else{
return $the_time;
}
}
}
}
}
}



2. Class implementation



Copy code

The code is as follows:
/*
* 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();
            }

                                                                                                                                                                                    exit();
        }

                                                                                                                            exit() ;
          }

}

}
//Usage examples of the class
/*

*

* Call the class output method

*

* $a = new Mygettime(time(),strtotime( '-25 month'));
* echo iconv('utf-8', 'gb2312', $a->index())?iconv('utf-8', 'gb2312', $a- >index()):iconv('utf-8', 'gb2312', 'current');
*
*/






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

www.bkjia.com

true

TechArticle1. Function implementation example 1: Copy the code as follows: 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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn