Home  >  Article  >  Backend Development  >  Simple method to calculate age in php

Simple method to calculate age in php

墨辰丷
墨辰丷Original
2018-05-29 17:25:212927browse

This article mainly introduces the method of simply calculating age in PHP. It uses custom functions to implement simple calculation functions for full years and virtual years. It involves PHP date and time related operation skills. Friends in need can refer to the following

The details are as follows:

/**
* $date是时间戳
* $type为1的时候是虚岁,2的时候是周岁
*/
function getAgeByBirth($date,$type = 1){
   $nowYear = date("Y",time());
   $nowMonth = date("m",time());
   $nowDay = date("d",time());
   $birthYear = date("Y",$date);
   $birthMonth = date("m",$date);
   $birthDay = date("d",$date);
   if($type == 1){
    $age = $nowYear - ($birthYear - 1);
   }else{$type == 2}{
    if($nowMonth<$birthMonth){
     $age = $nowYear - $birthYear - 1;
    }elseif($nowMonth==$birthMonth){
     if($nowDay<$birthDay){
      $age = $nowYear - $birthYear - 1;
     }else{
      $age = $nowYear - $birthYear;
     }
    }else{
     $age = $nowYear - $birthYear;
    }
   }
   return $age;
}

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php Using __call overload

php programming Kindeditor uploads pictures to add watermarks

PHP Method to dynamically compress js and css files

The above is the detailed content of Simple method to calculate age in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn