Home > Backend Development > PHP Tutorial > How to calculate age based on birthday in php

How to calculate age based on birthday in php

PHP中文网
Release: 2023-02-28 17:48:01
Original
2092 people have browsed it

php method to calculate age based on birthday.

The example in this article describes how PHP calculates age based on birthday. Share it with everyone for your reference. The details are as follows:

<?php 
function birthday($birthday){ 
  $age = strtotime($birthday); 
  if($age === false){ 
    return false; 
  } 
  list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age)); 
  $now = strtotime("now"); 
  list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now)); 
  $age = $y2 - $y1; 
  if((int)($m2.$d2) < (int)($m1.$d1)) 
    $age -= 1; 
  return $age; 
} 
echo birthday(&#39;1986-07-22&#39;); 
?>
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template