PHP calculates several just, several hours ago, a few days ago a few functions, class sharing

Source: Internet
Author: User
Tags current time diff min

  This article mainly introduces PHP calculation time several just, a few hours ago, a few days ago a few functions, class sharing, need friends can refer to the following

One, the function implementation Instance 1:     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 <) {  & nbsp  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) {//3 days         return floor ($dur/86400). ' Days ago ';        }else{        return $the _time        }      } &nbsp   Instance 2: Code is as follows: <?php function Format_date ($time) {  &nbsP $t =time ()-$time;     $f =array (        ' 31536000 ' => ' year ',       ' 2592000 ' => ' month ', &N Bsp       ' 604800 ' => ' Week ',         ' 86400 ' => ' Day ',       ' 3600 ' =&G t; ' Hours ',         ' => ' minutes ',       ' 1 ' => ' sec '    ;     foreach ($f as $k => $v)    {        if (0!= $c =floor ($t/(int) $k)) {            return $c $v. ' Before ';        }    }?>   Instance 3:     Code 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. " Hours 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 ';}} {    instance 4:     Code is as follows: function Time_tran ($the _time) {$now _time = date ("Y-m-d h:i:s", Time () +8*60*60); &nb Sp $now _time = strtotime ($now _time); $show _time = strtotime ($the _time); $dur = $now _time-$show _time; if ($dur < 0) {return $the _time; }else{if ($dur < a) {    return $dur. ' seconds ago '; }else{  &nbs P if ($dur < 3600) {   return floor ($dur/60). ' Minutes ago ';     }else{   if ($dur < 86400) {   return floor ($dur/3600). ' Hours ago ';     }else{   if ($dur < 259200) {//3 days        return floor ($dur/8640 0). ' Days ago ';    }else{       return $the _time;             }}}     II, the implementation of the class   code is as follows: <?php/*  * Author:solon ring &Nbsp;* time:2011-11-02  * Time calculation (year, month, day, time, minute, second)  * $createtime can be the current time  * $gettime the time you want to send in  */  C Lass mygettime{          function  __construct ($createtime, $gettime) {            $this->createtime = $createtime;             $this->gettime = $gettime;    }       function getseconds ()     {            Retur n $this->createtime-$this->gettime;        }       function getminutes ()        {       return ($this->createtime-$this->gettime)/(60);                 function getHours ()        {    &N Bsp  return ($this->createtime-$this->gettime)/(60*60);       &NBSP}         function getday ()       {        return ($this->createtime-$this->gettime)/(60*60*24);     & nbsp &NBSP}         function getmonth ()        {        return ($ this->createtime-$this->gettime)/(60*60*24*30);       &NBSP,          function getyear ()        {  &NBS P     return ($this->createtime-$this->gettime)/(60*60*24*30*12);                  function index ()        {            if ($this->getyear () > 1)             {    &NBS P            if ($this->getyear () > 2)               & nbsp     {       ,                 return date ("y-m-d",$this->gettime);                         exit ();                                   & nbsp Return Intval ($this->getyear ()). "Years ago";                 exit ();                            if ($this->getmonth () > 1)             {                return intval ($this->getmonth ()). " Months ago ";                 exit ();                            if ($this->getday () & Gt 1             {                Intval ($this ->getday ()). "Days Ago";             &NBSP   Exit ();                            if ($this->gethours () > 1)             {                return intval ($this->gethours ()). " Hours ago ";                 exit ();                            if ($this->getminutes () > 1)             {                return INTV Al ($this->getminutes ()). "Minutes Ago";                 exit ();                          if ($this->getseconds () > 1             {                Intval ($thi S->getseconds ()-1). "Seconds Ago";                 exit ();                     &NBSP}    }//class usages/*  *   * Invoke class Output mode  *  * $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 ');  *  */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.