This article mainly introduces the method for calculating the precision of php age to year, month, and day. it involves the related skills of php operation date and string, which is very simple and practical, for more information, see the next article. This article describes how to calculate the precision of php age to year, month, and day. it involves the related skills of php operation date and string. it is very simple and practical. For more information, see
This example describes how to calculate the precision of php age to year, month, and day. Share it with you for your reference. The details are as follows:
$ Ni) {$ not_birth = 1; $ tmp = array ($ byear, $ bmonth, $ bday); list ($ byear, $ bmonth, $ bday) = array ($ year, $ month, $ day); list ($ year, $ month, $ day) = $ tmp; list ($ bi, $ ni) = array ($ ni, $ bi) ;}$ years = 0; while ($ bi + 10000) <= $ ni) {// Get The Age $ bi + = 10000; $ years ++; $ byear ++;} // Get The Age and discard the year list ($ m, $ d) = $ this-> getMD (array ($ year, $ month, $ day), array ($ byear, $ bmonth, $ bday )); return array ('year' => $ yea Rs, 'month' => $ m, 'day' => $ d, 'not _ birth' => $ not_birth );} /*** can only be used for calculation within one year * @ param type $ ymd * @ param type $ bymd */public function getMD ($ ymd, $ bymd) {list ($ y, $ m, $ d) = $ ymd; list ($ by, $ bm, $ bd) = $ bymd; if ($ m. $ d) <($ bm. $ bd) {$ m + = 12;} $ month = 0; while ($ bm. $ bd) + 100) <= ($ m. $ d) {$ bm ++; $ month ++;} if ($ bd <= $ d) {// same month $ day = $ d-$ bd;} else {// less than one month $ mdays = $ bm> 12? $ This-> _ getMothDay (++ $ by, $ bm-12): $ this-> _ getMothDay ($ by, $ bm ); $ day = $ mdays-$ bd + $ d;} return array ($ month, $ day);} private function _ getMothDay ($ year, $ month) {switch ($ month) {case 1: case 3: case 5: case 7: case 8: case 10: case 12: $ day = 31; break; case 2: $ day = (intval ($ year % 4 )? 28: 29); // The other 28-day break values that can be exhausted by 4 are 29 days; default: $ day = 30; break ;}return $ day ;}} $ cage = new Age (); $ test = array ('2017-06-12 ', '2017-07-13', '2017-08-16 ', '2017-10-10 ', '2017-10-13 ', '2017-10-15', '2017-11-9 ', '2017-11-22', '2017-11-22 ', '2017-8-22 ', '2017-10-13 ',); echo date ('Y-m-D'); echo''; Foreach ($ test as $ v) {$ tmp = $ cage-> calAge ($ v); echo $ v,': ', $ tmp ['Year'], 'year', $ tmp ['month'], 'month', $ tmp ['day'], 'day ',';', $ tmp ['not _ birth '],'
';} Echo''
Output:
/* Running result: April 1 March 0: 25-January 27 days; 0: 25-days; 0: 25-days; 0: 25-days; 0 1990-10-10: 25-0-3 days; 0 1990-10-13: 25 year 0 month 0 day; 0 1990-10-15: 24 year November 28 day; 0 1990-11-9: 24 year November 4 day; 0 1990-11-22: 24 year October 21 day; 0: 1-January 9 days; 1-October 9: 0-days; 1-: 1-year, 0-month, 0-day; 1 **/