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 =>28,3=>31,4=>30,5=>31,6=>30,7=>31,8=>31,9=>30,10=>31,11=>30,12=>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 ');}? >