PHP transforms the time into several functions, class sharing, a few minutes ago, a few hours ago, a few days ago.

Source: Internet
Author: User
Tags diff
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

  • Related Article

    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.