PHP Computing age accurate to month-day _php tips

Source: Internet
Author: User
Tags echo date explode php programming

The example in this article describes the way in which PHP calculates age-accurate to year-month dates. Share to everyone for your reference. 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 {/** * calculating the ages to date * @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 <) {$bmonth = ' 0 ' $bmonth;
    } if ($bday <) {$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) {//First Age $bi + + 10000;
      $years + +;
    $byear + +;
    }//gets older and discards the year 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 for one year calculation * @param type $YMD * @param type $BYMD/Public function GETMD ($YMD, $bymd) {lis
    T ($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;
      else {//less one months $mdays = $BM > $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: $day = 31;
      Break Case 2: $day = (intval ($year% 4)? 28:29);
      Can be 4 in addition to the 29 days of 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 ' <pre> ';
  foreach ($test as $v) {$tmp = $cage->calage ($v);
echo $v, ': ', $tmp [' Year '], ' years ', $tmp [' Month '], ' month ', $tmp [' Day '], ' days ', '; ', $tmp [' Not_birth '], ' <br> ';
 
 
Echo ' </pre> ';
  /* Run Results: 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 * 
 */ 

Hopefully this article will help you learn about PHP programming.

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.