Php computing age accurate to Year Month Day, php computing Year Month Day _ PHP Tutorial

Source: Internet
Author: User
Tags learn php programming
Php calculates the age accurate to year, month, and day, and php calculates the year, month, and day. Php calculates the age accurate to the year, month, and day. This document describes how php calculates the age accurate to the year, month, and day. Share it with you for your reference. The details are as follows: php ** T php calculates the age accurate to year, month, and day, and php calculates the year, month, and day.

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:

<? 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.

Examples in this article describe how php calculates the accuracy of age to year, month, and day. Share it with you for your reference. Details: php/** T...

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.