Home  >  Article  >  Backend Development  >  PHP get time difference

PHP get time difference

高洛峰
高洛峰Original
2016-10-21 10:20:561948browse

PHP obtains the time difference. This is a function that we often use during the development process. For example, if we want to check data for a certain time period, we will use this function.


Example 1:

/**
 * 功能:获取时间差
 * @param int $time
 * @return string 时间差值
 */
function tranTime($time) {
    $rtime = date("m-d H:i",$time);
    $htime = date("H:i",$time);
 
    $time = time() - $time;
 
    if ($time < 60) {
        $str = &#39;刚刚&#39;;
    }
    elseif ($time < 60 * 60) {
        $min = floor($time/60);
        $str = $min.&#39;分钟前&#39;;
    }
    elseif ($time < 60 * 60 * 24) {
        $h = floor($time/(60*60));
        $str = $h.&#39;小时前 &#39;.$htime;
    }
    elseif ($time < 60 * 60 * 24 * 3) {
        $d = floor($time/(60*60*24));
        if($d==1)
           $str = &#39;昨天 &#39;.$rtime;
        else
           $str = &#39;前天 &#39;.$rtime;
    }
    else {
        $str = $rtime;
    }
    return $str;
}

Example 2:

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.&#39;秒前&#39;;
    }else{
     if($dur < 3600){
      return floor($dur/60).&#39;分钟前&#39;;
     }else{
      if($dur < 86400){
       return floor($dur/3600).&#39;小时前&#39;;
      }else{
       if($dur < 259200){//3天内
        return floor($dur/86400).&#39;天前&#39;;
       }else{
        return $the_time;
       }
      }
 }

Example 3:

function format_date($time){
    $t=time()-$time;
    $f=array(
        &#39;31536000&#39;=>&#39;年&#39;,
        &#39;2592000&#39;=>&#39;个月&#39;,
        &#39;604800&#39;=>&#39;星期&#39;,
        &#39;86400&#39;=>&#39;天&#39;,
        &#39;3600&#39;=>&#39;小时&#39;,
        &#39;60&#39;=>&#39;分钟&#39;,
        &#39;1&#39;=>&#39;秒&#39;
    );
    foreach ($f as $k=>$v)    {
        if (0 !=$c=floor($t/(int)$k)) {
            return $c.$v.&#39;前&#39;;
        }
    }
}

Example 4:

function formatTime($date) {
$str = &#39;&#39;;
$timer = strtotime($date);
$diff = $_SERVER[&#39;REQUEST_TIME&#39;] - $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){
return $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 &#39;刚刚&#39;;
}
}
}else{
return &#39;刚刚&#39;;
}
}
}else{
return &#39;刚刚&#39;;
}
}
}

Example 5:

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.&#39;秒前&#39;; 
}else{
    if($dur < 3600){
   return floor($dur/60).&#39;分钟前&#39;; 
    }else{
   if($dur < 86400){
   return floor($dur/3600).&#39;小时前&#39;; 
   }else{
   if($dur < 259200){//3天内
       return floor($dur/86400).&#39;天前&#39;;
   }else{
       return $the_time; 
   }
   }
    }
}
}
}

Example 6:

/*
 * 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();
            }
if($this->getMinutes() > 1)
            {
                return intval($this->getMinutes())." 分钟前";
                exit();
            }
if($this->getSeconds() > 1)
            {
                return intval($this->getSeconds()-1)." 秒前";
                exit();
            }
}
}
//类的使用实例
/*
 *
 * 调用类输出方式
 *
 * $a = new Mygettime(time(),strtotime(&#39;-25 month&#39;));
 * echo iconv(&#39;utf-8&#39;, &#39;gb2312&#39;, $a->index())?iconv(&#39;utf-8&#39;, &#39;gb2312&#39;, $a->index()):iconv(&#39;utf-8&#39;, &#39;gb2312&#39;, &#39;当前&#39;);
 *
 */


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