This article mainly introduces PHP calculation time a few ago, a few hours ago, a few days ago several functions, class sharing, the need for friends can refer to the next
First, the function realization
Example 1:
Example 2:
Example 3:
echo time (); $f =array ( ' 31536000 ' = ' year ', ' 2592000 ' + ' month ', ' 604800 ' + ' ' Week ', ' 86400 ' and ' Day ', ' 3600 ' = ' + ' hours ', ' + ' = ' minutes ', ' 1 ' = ' s ' ; foreach ($f as $k = + $v) { if (0! = $c =floor ($t/(int) $k)) { return $c. $v. ' ago '; } } Echo format_date ("1404600000"); ? >
Example 4:
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 '; }}} Echo Formattime ("2014-7-8 19:22:01"); ?>
Second, the realization of the class
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 ($thi S->getyear () > 2) {return date ("y-m-d", $this->gettime); Exit (); } return Intval ($this->getyear ()). "Years ago"; Exit (); } if ($this->getmonth () > 1) {return intval ($this->getmonth ()). "Months ago"; Exit (); } if ($this->getday () > 1) {return intval ($this->getday ()). "Days Ago"; Exit (); } if ($this->gethours () > 1) {return intval ($this->gethours ()). "Hour Ago" ; Exit (); } if ($this->getminutes () > 1) {return intval ($this->getminutes ()). " Minutes ago "; Exit (); } if ($this->getseconds () > 1) {return intval ($this->getseconds ()-1). " Seconds ago "; Exit (); }}}//class using instance/* * * Invoke 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 ' ); * */
Reprint: http://blog.csdn.net/china_skag/article/details/37569505