PHP calculates age (years old) based on birthday

PHP中文网
Release: 2023-03-01 06:42:01
Original
4826 people have browsed it

PHP calculates age (one year old) based on birthday

<?php/**
 * 根据时间戳计算年龄
 * @param $birth
 * @return mixed
 */functionhowOld($birth) {list($birthYear, $birthMonth, $birthDay) = explode(&#39;-&#39;, date(&#39;Y-m-d&#39;, $birth));
    list($currentYear, $currentMonth, $currentDay) = explode(&#39;-&#39;, date(&#39;Y-m-d&#39;));
    $age = $currentYear - $birthYear - 1;
    if($currentMonth > $birthMonth || $currentMonth == $birthMonth && $currentDay >= $birthDay)
        $age++;

    return$age;
}
?>
Copy after login

The above introduces PHP to calculate age (one year old) based on birthday, including the content. For more related content, please pay attention to the PHP Chinese website (www.php.cn) !

Related articles:

How to calculate age based on birthday in php

Example of how to calculate age/zodiac sign/constellation in php based on birthday

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!