PHP to calculate the age of accurate to the date of the program

Source: Internet
Author: User
Tags echo date


This article mainly introduced the PHP calculation age accurate to year month day the method, involves the PHP operation date and the string correlation skill, very simple practical, needs the friend to be possible to refer to under

This article is an example of how the PHP calculation age is accurate to the date of year. Share to everyone for your reference. Specifically 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) {//Age First
$bi + 10000;
$years + +;
$byear + +;
}//get older and leave years behind
List ($m, $d) = $this->getmd (Array ($year, $month, $day), Array ($byear, $bmonth, $bday));
Return Array (' => $years, ' month ' => $m, ' Day ' => $d, ' Not_birth ' => $not _birth);
}

/**
* Can only be used in one year calculation
* @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) + <= ($m. $d)) {
$BM + +;
$month + +;
}
if ($bd <= $d) {//same place one months
$day = $d-$bd;
{//Less one months
$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); Can be removed by 4 for 29 days other 28 days
Break
Default
$day = 30;
Break
}
return $day;
}

}

$cage = New Age ();
$test = Array (
' 1990-06-12 ',
' 1990-07-13 ',
' 1990-08-16 ',
' 1990-10-10 ',
' 1990-10-13 ',
' 1990-10-15 ',
' 1990-11-9 ',
' 1990-11-22 ',
' 2016-11-22 ',
' 2016-8-22 ',
' 2016-10-13 ',
);
echo Date (' y-m-d ');
Echo '
';
foreach ($test as $v) {
$tmp = $cage->calage ($v);
echo $v, ': ', $tmp [' Year '], ' years ', $tmp [' Month '],
' Month ', $tmp [' Day '], ' days ', '; ', $tmp [' Not_birth '], '
';
}
Echo '
' ;


/*
Run Result:
2015-10-13
1990-06-12: April 25 1 days; 0
1990-07-13: March 25 0 days; 0
1990-08-16: January 25 27 days; 0
1990-10-10:250月 3 days; 0
1990-10-13:250月 0 days; 0
1990-10-15: November 24 28 days; 0
1990-11-9: November 24 4 days; 0
1990-11-22: October 24 21 days; 0
2016-11-22:1 year January 9 days; 1
2016-8-22:0 year October 9 days; 1
2016-10-13:1 year 0月 0 days; 1

*
*/
I hope this article will help you learn the PHP program

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.