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 example in this article. Share it with you for your reference. The details are as follows:
<? Php/** To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */class Age {/*** calculate the accuracy of Age to year, month, and day * @ param type $ birthday * @ return array */public function calAge ($ birthday) {list ($ byear, $ bmonth, $ bday) = explode ('-', $ birthday); list ($ year, $ month, $ day) = explode ('-', date ('Y-m-d '); $ Bmonth = intval ($ bmonth); $ bday = intval ($ bday); if ($ bmonth <10) {$ bmonth = '0 '. $ bmonth;} if ($ bday <10) {$ bday = '0 '. $ bday;} $ bi = intval ($ byear. $ bmonth. $ bday); $ ni = intval ($ year. $ month. $ day); $ not_birth = 0; if ($ bi> $ 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' => $ years, '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) {// The 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'
';/* Running result: April 1: 25-March 0 days; 0 January 27: 25-days; 0: 25-days; 0: 25-days; 0 1990-10-10: 25 years 0 months 3 days; 0 1990-10-13: 25 years 0 months 0 days; 0 1990-10-15: 24 years November 28 days; 0 1990-11-9: 24 years November 4 days; 0 1990-11-22: 24-October 21 days; 0 January 9 days; 1 2016-8-22: 0-October 9 days; 1 2016-10-13: 1 year 0-0 days; 1 **/
I hope this article will help you learn php programming.