PHP Age calculation gets two timestamp difference of age Month Day display

Source: Internet
Author: User

Because you need to display the age of the object, on the Internet to find a few look not too ideal, I wrote a, get two time stamp of the age of the function, the result is x years x months x days, the month is zero, does not show the date, the principle is: Month day subtraction, the number is not enough, by the year and month of the carry the number, finally get the time value.

<? php/* * ============================== *  This method is provided by  mantye     *  http://my.oschina.net/u/223350  *  @date  2014-07-22 * =========================== === *  @description      get two time stamp age  *  @before            small timestamp  *  @after             larger timestamp  *  @return  str      return age difference y years m months D days **/function  Datediffage ($before,  $after)  { if  ($before > $after)  {   $b  =  GETDATE ($after);   $a  = getdate ($before); } else {   $b  =  getdate ($before);   $a  = getdate ($after); }  $n  = array (1=>31,2 =&GT;28,3=&GT;31,4=&GT;30,5=&GT;31,6=&GT;30,7=&GT;31,8=&GT;31,9=&GT;30,10=&GT;31,11=&GT;30,12=&GT;31);  $y = $m = $d =0; if  ($a[' Mday ']>= $b [' Mday '])  { //day subtraction is positive   if  ($a [' mon ']>= $b [' mon '])  {//moon phase reduced to positive      $y = $a [' Year ']-$b [' Year ']; $m = $a [' mon ']-$b [' Mon '];  }  else { //the moon phase minus negative, Borrowing years     $y = $a [' Year ']-$b [' Year ']-1; $m = $a [' mon ']-$b [' mon ']+12;  }   $d = $a [' Mday ' ]-$b [' Mday ']; } else {  //days minus negative, borrow month   if  ($a [' mon ']==1)  { //1 month , borrowing years     $y = $a [' Year ']-$b [' Year ']-1; $m = $a [' mon ']-$b [' mon ']+12; $d = $a [' mday ']-$b [' Mday ']+ $n [12];   }  else {   if  ($a [' mon ']==3) &NBSP;{&NBSP;//3 month, judging leap years get February days       $d = $a [' mday ']-$b [' Mday ']+ ($a [' Year ']%4==0?29:28);    }   else  {     $d = $a [' mday ']-$b [' Mday ']+ $n [$a [' Mon ']-1];   }    if  ($a [' mon ']>= $b [' mon ']+1)  { //borrow months, the moon phase is reduced to positive      $y = $a [' Year ']-$b [' Year ']; $m = $a [' mon ']-$b [' Mon '']-1;   }   else { //borrow month, the moon phase minus negative, borrow years      $y = $a [' Year '] -$b [' Year ']-1; $m = $a [' mon ']-$b [' Mon ']+12-1;   }  } } return  ($y ==0? ': $ Y. ' old '). ($m ==0? ': $m. ' months '). ($d ==0? ': $d. ' Day ');}? >

 

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.